解决方案:重写router的push方法
// 创建路由实例
let router = new Router({
base: process.env.VUE_APP_APP_NAME ? process.env.VUE_APP_APP_NAME : "/",
// history模式地址栏不会显示#号,hash模式会显示#号,默认值是hash
mode: 'history', // 去掉url中的#
scrollBehavior: () => ({y: 0}),
routes: constantRoutes
})
// 保存原本的push函数
let routerPush = Router.prototype.push;
// 重写push方法
Router.prototype.push = function push(location) {
return routerPush.call(this, location).catch(err => err)
}
// 导出路由实例
export default router