【发布时间】:2021-07-20 11:30:17
【问题描述】:
我想在 Vue 3 的 vue-router 中编写一些复杂的保护逻辑,以保护根据 store 和我提供的其他模块进入一些路由。例如,我想检查是否存在用户个人资料信息:
router.afterEach((to, from) => {
console.log('store: ', useStore());
const puex = usePuex();
puex.isReady().then(() => {
const me = puex.me.compute();
watch(me, (...params) => console.log('router: ', ...params));
});
});
在上面的代码中,useStore 和 usePuex 都尝试从 Vue 应用程序注入 store 和 puex 实例,这些实例是在 main.js 引导程序中使用时提供的。但是两个 use 函数都返回 undefined,我猜这个范围内的注入会搜索不存在应用程序级提供的实例的不同位置。
那么如何将它们注入到路由器文件中,或者换句话说,如何在此处使用 useStore 和 usePuex 获取 store 和 puex 实例?
【问题讨论】:
标签: vue.js vue-router vuejs3 vue-composition-api