【发布时间】:2022-01-12 14:45:29
【问题描述】:
我是 vuejs 新手,想在组件加载前做个检查,但不知道怎么做。
我想要做的是如果"this.$store.getters.user.id" 和"this.$route.params.id" 在组件加载之前不相等
"this.$router.push({name: 'home', params: {id" : this.$store.getters.user.id}})"
{
path: '/:id',
name: 'home',
component: () => import('../Views/Admin/Home.vue'),
meta: {
guard: 'auth'
},
children: [
{
path: 'admin',
component: () => import('../Views/Admin/HomeProfile.vue'),
name: 'admin',
meta: {
guard: 'auth'
},
}
]
},
router.beforeEach((to, from, next) => {
if (store.getters.user) {
if (to.matched.some(route => route.meta.guard === 'guest')) next({name: 'home'})
else next();
} else {
if (to.matched.some(route => route.meta.guard === 'auth')) next({name: 'login'})
else next();
}
})
【问题讨论】:
-
我该怎么做?路由器这样
-
这可能会有所帮助:stackoverflow.com/a/70662676/10975709 这有点相关。在您的情况下,只需检查 store 而不是
checkAuth函数并使用meta而不是路由名称。 -
不,它根本没有帮助
标签: vue.js