Vue beforeEach 拦截判断 PC、移动端

项目中用到 beforeEach 需要注意的点

由于 H5(内嵌) 项目中只能在微信浏览器跟安卓IOS内使用,所以在路由守卫做了拦截,做的时候发现路由守卫陷入了死循环(离谱)代码如下

if (!store.state.isAndroid && !store.state.isWeixin && !store.state.isIOS) {
    next({ path: '/error' })
}  else {
    next()
}

之后查阅文档发现

next()不会重新触发beforeEach()函数

next({ path: '/xxx' }) 跳到不同的地址都会再次执行 router.beforeEach 钩子函数。

if (to.name == '404') {
    next()
  } else if (!store.state.isAndroid && !store.state.isWeixin && !store.state.isIOS) {
    next({ path: '/error' })
  }

posted @ 2021-11-05 16:44  Hekiss  阅读(280)  评论(0编辑  收藏  举报