例如要实现用户登录后跳转,很自然的想到将login这个异步操作放到action,登录成功后再触发mutation来修改应用状态(设置已登录状态),但是之后如何触发路由跳转呢,这点我一直想不通……
8 回答
Qyou
TA贡献8条经验 获得超4个赞
import Vue from 'vue'; import VueRouter from 'vue-router'; import filters from './filters'; import routerMap from './routers'; import FastClick from 'fastclick'; Vue.use(VueRouter); $.ajaxSettings.crossDomain = true; //实例化Vue的filter Object.keys(filters).forEach(k => Vue.filter(k, filters[k])); //实例化VueRouterlet router = new VueRouter({ hashbang: true, history: false, saveScrollPosition: true, transitionOnLoad: true}); //登录中间验证,页面需要登录而没有登录的情况直接跳转登录router.beforeEach((transition) => { //处理左侧滚动不影响右边 $("html, body, #page").removeClass("scroll-hide"); FastClick.attach(document.body); if (transition.to.auth) { if (localStorage.userId) { transition.next(); } else { var redirect = encodeURIComponent(transition.to.path); transition.redirect('/login?redirect=' + redirect); } } else { transition.next(); }}); let app = Vue.extend({});routerMap(router);router.start(app, "#app");
添加回答
举报
0/150
提交
取消