2017-10-19 173 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 
    }, 
... 
] 
} 
...