查看: 70|回覆: 0

导航守卫。404页面设置

[複製鏈接]

2

主題

1

回帖

0

積分

热心网友

金币
1
閲讀權限
220
精華
0
威望
0
贡献
0
在線時間
0 小時
註冊時間
2008-5-7
發表於 2020-1-15 14:09:00 | 顯示全部樓層 |閲讀模式

全局导航守卫:(在router.js中添加)

router.beforeEach((to,from,next)=>{
  if(to.name!=='login'){
    if(localStorage.getItem('userid')){
      next()
    }else{
      next('login')
    }
}else{
    next()
}
})

组件内导航守卫:(在组件内添加)

beforeRouteEnter(to,form,next){
    if(localStorage.getItem('userid')){
      next()
    }else{
      alert('请先登录')
      next('login')
    }
  },

404页面设置:(在router.js中设置)

{ //输错路由重定向到登陆页面
    path: "*",
    redirect: "404"
  }
回覆

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即注册

本版積分規則

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部