2017-09-10 40 views

回答

0

我會拋出代碼然後解釋。

const router = new VueRouter({ 
    mode: 'history', 
    routes, 
}); 

router.beforeEach((to, from, next) => { 
    if (to.matched.length === 0) { 
    window.location.reload(); 
    } 
    next(); 
}); 

router.beforeEach在用三個參數加載路由之前調用。 to, from and next。接下來是一個回調函數。
這裏的關鍵是to參數中的匹配屬性。它包含一組匹配的路線。如果找不到匹配的路由,則to.matched數組的長度將變爲0,這是我用於做出決定的屬性。

vue-router應該有一種其他類型的財產。但是,這是一場我將在另一天戰鬥的戰鬥。