经常会碰到 a-b-c-a 返回跨级路由或指定路由,只需在c页面加上以下就是处理方法
mounted(){ if (window.history && window.history.pushState) { history.pushState(null, null, document.URL) window.addEventListener('popstate', this.onCloseModal, false) } }, destroyed () { window.removeEventListener('popstate', this.onCloseModal, false) }, methods: { onCloseModal(){ // 处理逻辑(非固定,可自行发挥) this.$router.go(-2) // 或者 this.$router.push('/a') } }
上述利用了监听历史记录方法,实际上历史记录还是存在的
//vue路由跳转replace方式就是不记录路由的跳转历史,所以b跳转c的时候用replace返回就会返回到a页面 this.$router.replace({ path: '' }); // 下面是处理不管经过多少层跳转,返回到指定页面方法 let backlen = window.history.length; window.history.go(-backlen); this.$router.replace({ path: '' });
vue路由跳转处理 a到b,b到c,c返回a
于 2022-01-18 16:38:58 首次发布