最近項目在sentry上看到一些重複點擊路由的錯誤。
很多網站左側是菜單欄,但是重複點擊會在看到如下的錯誤報下面的異常。
NavigationDuplicated {_name: "NavigationDuplicated", name: "NavigationDuplicated", message: "Navigating to current location ("/crm/agent-account") is not allowed", stack: "Error↵ at new NavigationDuplicated (webpack:///…odules/vue-loader/lib??vue-loader-options:105:20)"}
這是由於push的時候,router會有同樣的路由,會導致報錯。
https://github.com/vuejs/vue-router/issues/2872
但是發現router.push並不是返回的promise,所有用catch並不能捕獲到。
此時只能在最外面,如果發現是相同的路由的話,直接在click的時候進行過濾
onClick({ node }) {
if(this.$router.currentRoute.name === node.name){
console.log('重複了')
return
}
this.$router.push({
name: node.name,
})
},