在router的index.js里面寫,在use之前,如果加上以下代碼,報錯‘Cannot read properties of undefined (reading ‘catch’) at VueRouter.push ’那就是vue-router的版本問題,安裝高一點的版本即可3.1.6以上
// 保存原來的push函數
const originalPush = Router.prototype.push;
// 重寫push函數
Router.prototype.push = function push(location) {
// return originalPush.call(this, location).catch(err => err);
// 這個if語句在跳轉相同路徑的時候,在路徑末尾添加新參數(一些隨機數字)
// 用來觸發watch
if(typeof(location)=="string"){
var Separator = "&";
if(location.indexOf('?')==-1) { Separator='?'; }
location = location + Separator + "random=" + Math.random();
}
// 這個語句用來解決報錯
// 調用原來的push函數,并捕獲異常
return originalPush.call(this, location).catch(error => error);
};
Vue.use(Router);