【发布时间】:2017-07-23 14:32:53
【问题描述】:
如何在 beforeEnter 中访问通过 store 操作异步检索的 store 数据?
import store from './vuex/store';
store.dispatch('initApp'); // in here, async data will be fetched and assigned to the store's state
// following is an excerpt of the routes object:
{
path: '/example',
component: Example,
beforeEnter: (to, from, next) =>
{
if (store.state.asyncData) {
// the above state is not available here, since it
// it is resolved asynchronously in the store action
}
}
}
这在第一个页面加载或页面重新加载之后尤其重要,此时正在获取初始化数据并且路由器需要等待该数据以允许用户访问该页面。
路由器是否可以“等待”获取数据? 或者结合异步 vuex 存储数据处理导航守卫的最佳方式是什么?
(哦,预先填充“asyncData”不是解决方案,因为 beforeEnter 钩子需要根据数据库中的真实数据而不是默认数据做出决定)
【问题讨论】:
-
你在哪里调用 vuex 动作来加载状态?
-
通过
store.dispatch('initApp');
标签: javascript vue.js vuex vue-router