在vue-router解析器中使用vuex状态

时间:2020-06-25 13:04:24

标签: vue.js vuex vue-router

我正在尝试将解析器添加到使用Vuex和Vue Router的当前项目中。

到目前为止,除了我尝试访问在路由上定义的beforeEnter的解析器中的状态以外,所有其他功能都可以正常运行。

这是我当前的设置:

{
    path: '/dashboard',
    name: 'dashboard',
    component: () => import('./views/Dashboard.vue'),
    beforeEnter: DashboardResolver
}

这是解析器:

import axios from 'axios';
import store from '../store/auth.module';

export default async (to, from, next) => {
    to.meta.user = await axios.get(`/users/${store.state.currentUser.id}`);

    next();
}

我无法理解的疯狂的事情是,如果我做console.log(store.state);然后得到状态,但是如果我执行以下操作,则在同一行console.log(store.state.currentUser);上返回空值,这就是我要解决的问题面对这里。

0 个答案:

没有答案
相关问题