2017-10-19 189 views
0

我想为我的孩子路线定义一个beforeEnter后卫,但我没有成功。这里是我的路由配置:Vue-router:beforeEnter guard对于儿童路径无法正常工作

... 
    { 
    path: '/', 
    component: App 
    beforeEnter: (to, from, next) -> 
     # This block is only reached when I refresh the page 

    children: [ 
     { 
     name: 'component1', 
     path: '/component1', 
     components: component1 
     }, 
     { 
     name: 'path2', 
     path: '/path2', 
     components: component2 
     }, 
    ... 
    ] 
    } 
    ... 

一切工作正常,当我刷新页面,或直接在浏览器(例如:BASE_PATH /路径2)插入的URL。但是当我点击重定向到path1或path2的路由器链接时,beforeEnter守护程序不会执行。

我明白什么错了吗?我是否需要为每个孩子设置一个beforeEnter后卫?

回答

0

我发现是使用beforeEach后卫,而不是beforeEnter的最佳解决方案。

beforeEnter是每个路线警卫,然后它只适用于父路线,但不适用于孩子。

0

尝试添加beforeRouteUpdate挂钩,即

... 
{ 
path: '/', 
component: App 
beforeEnter: (to, from, next) -> 
    # This block is only reached when I refresh the page 
beforeRouteUpdate: (to, from, next) -> 
    # This will called when you use router-links 
children: [ 
    { 
    name: 'component1', 
    path: '/component1', 
    components: component1 
    }, 
    { 
    name: 'path2', 
    path: '/path2', 
    components: component2 
    }, 
... 
] 
} 
...