vue中main.js文件 ,這個文件夾中進行操作,因為在這個頁面注冊的所有組件,原型上面的方法也都在這個頁面上
var flag; // 首先定義一個開關 router.beforeEach((to, from, next) => { if (to.path == '/game/aaaa' || to.path == '/game/bbbb') { // 當進入這個頁面進行記錄 flag = true; // 改變信號 next(); } else { if (flag) { // 如果路由發生變化判斷信號 Vue.prototype.$confirm('您確定要退出此頁面?', '提示', { confirmButtonText: '確定', cancelButtonText: '取消', type: 'warning' }).then(() => { next() flag = false; }) } else { next() } } });
這里使用的 if 判斷可以寫一個方法 然后封裝一個函數使用 swich case 進行判斷然后函數調用,這里使用 vue-router 中的路由前置守衛 ,里面有三個參數 ,next()在這里是一個函數 就是方法向下執行的一個方法