解决的核心思想就是:不允许用户访问到带 from 参数的地址,如果收到 from 参数,就去掉 from 重新拼接 url ,重定向到不带 from 的 url
router.beforeEach((to, from, next) => { if (window.location.href.includes('from')) { if (to.path == '/productIndex') { window.location.replace(window.location.origin + '/#/productIndex') } else if (to.path == '/productDetail') { window.location.replace(window.location.origin + "/#" + to.fullPath) } } next() })
【注意】replace方法: 该方法通过指定 URL 替换当前缓存在历史里(客户端)的项目,因此当使用 replace 方法之后,你不能通过“前进”和“后退”来访问已经被替换的 URL。