vue项目优化--如果地址栏输入不存在的路由,直接跳转至404页面

1,068 阅读1分钟

如果在地址栏手动输入不存在的地址,直接跳转至404,在路由拦截里面做判断,写个demo:

router.beforeEach((to, from, next) => {
  if (store.state.CandyHouse.token) {
    if (to.path == '/login') {
      next({path: '/'});
    } else {
      if (to.matched.length === 0) {
        next("/404");
      } else {
        next();
      }
    }
  } else {
    if (to.path == '/login') {
      next();
    } else {
      next({path: '/login'});
    }
  }
})