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()在这里是一个函数 就是方法向下执行的一个方法