在进行vue商城项目 同页跳同页 参数改变时 报错
//本页路由
/inmall?id=46
//跳转
/inmall?id=30
//方法
goinne(id) {
this.$router.push({path: `/inmall`, query: {id: id}})
},
// 报错
vue-router.esm.js?fe87:2051 Uncaught (in promise) NavigationDuplicated {_name: "NavigationDuplicated", name: "NavigationDuplicated", message: "Navigating to current location ("/inmall?id=36") is not allowed", stack: "Error↵ at new NavigationDuplicated (webpack-int…node_modules/vue/dist/vue.runtime.esm.js:1853:26)"}
解决方法
- 重写路由的push方法
- 解决,相同路由跳转时,报错
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location, onResolve, onReject) {
if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject);
return originalPush.call(this, location).catch((err) => err);
};
在router.js中添加就好了
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
return originalPush.call(this, location).catch(err => err)
}