在主要方面,我具有以下启动功能:
new Vue({
el: "#app",
render: h => h(App),
store,
router,
mounted() {
this.startUp();
},
methods: {
startUp() {
store.dispatch('auth/startUp')
.catch(err => console.log(err));
}
},
});
问题是startUp
是在router.beforeEach((to, from, next)
之后启动的。在beforeEach
中,我需要验证:
如果用户决定刷新页面,则启动脚本应刷新用户状态。加载后,将使用Vuex管理状态。
我在main.js中定义了我的路线。 store.state.auth.user
为空,因为启动端点在router.beforeEach((to, from, next)
之后结束。
// configure router
const router = new VueRouter({
routes, // short for routes: routes
linkExactActiveClass: "nav-item active"
});
router.beforeEach((to, from, next) => {
if (store.state.auth.user && store.state.auth.user.setup) {
const user = store.state.auth.user;
var path = to.path !== "/setup" && to.path !== "/logout" ? "/setup" : false;
if (path) {
next({
path: path,
query: {
redirect: to.fullPath
}
});
}
}
});