vue移动端 安卓按钮返回 和自己重定向的路由利用钩子里面的导航守卫beforeRouteLeave实现返回按钮不返回上一次路由,返回到自己指定的路由

xiaoxiao2021-02-28  6

最近写移动端的时候遇到一些问题,想分享到自己的博客上,话不多说上代码

goCalendar(router){ this.flag = true; this.$router.push({path: router, query: {params: JSON.stringify(this.time)},replace:true}); }, 这里是配置好的一个路由

这里先介绍一下导航守卫beforeRouteLeave的一些参数

to:router 即将要进入的路由对象

from:router  当前导航正要离开的路由

next()进行管道中的下一个钩子

最后要确保调用next方法,否则钩子不会被resolved

下面进入正题

beforeRouteLeave (to, from, next) { if (this.flag) {//这是一个点击事件 //说明我是点击事件的跳转 next(); //正常执行手机返回键也是正常返回上一个路由 } else { if (from.path !== 'site') { //要离开的路由不是site next();// } //说明我是返回事件的跳转 //next(false); this.$router.push({path: 'site'})//返回键要返回的路由 } } 这样就实现了路由的固定跳转了
转载请注明原文地址: https://www.6miu.com/read-2500213.html

最新回复(0)