具有Navigation Guards的Vue路由器-next()不起作用

时间:2019-10-15 09:18:33

标签: javascript vue.js vue-router vue-cli

使用导航卫士保护Vue路线。 导航卫士在加载或刷新时正在工作,但是 使用next()访问路由时,<router-link>功能不起作用。

<router-link to="{ name: 'page1' }" >Page 1</router-link>

导航保护代码。

{
   path: '/page1',
   component: page1,
   name: 'page1',
   meta: { auth: true },
   beforeEnter: (to, from, next) => {
     if (!store.state.roles.includes('is_superadmin')) {
            if (!store.state.firm_permissions.includes('can_have_fire_contractors')) {
                 console.log('success page1')
                 next({
                    name: "page0"
                 })
             } else {
                 next()
             }
      }
      next()
    },
 }

我可以看到console.log('success page1'),但是next()无效...

1 个答案:

答案 0 :(得分:1)

return之后添加next()

beforeEnter: (to, from, next) => {
     if (!store.state.roles.includes('is_superadmin')) {
            if (!store.state.firm_permissions.includes('can_have_fire_contractors')) {
                 console.log('success page1')
                 next({
                    name: "page0"
                 })
                 return // Add this
             } else {
                 next()
             }
      }
      next()
    },