如何在vue-router中访问异步存储数据以在beforeEnter挂钩中使用?

JavaScript

猴子神无

2020-03-13

如何访问beforeStore中通过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挂钩需要根据数据库中的实际数据(而非默认数据)做出决定)

第1464篇《如何在vue-router中访问异步存储数据以在beforeEnter挂钩中使用?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

0个回答

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android