鍍金池/ 問(wèn)答/HTML/ VUE路由攔截后登錄成功,瀏覽器點(diǎn)擊返回按鈕還能回到登錄頁(yè)面怎么處理?

VUE路由攔截后登錄成功,瀏覽器點(diǎn)擊返回按鈕還能回到登錄頁(yè)面怎么處理?

clipboard.png

這是路由進(jìn)來(lái)的時(shí)候,攔截后去到登錄頁(yè)面。

clipboard.png

登錄成功后,進(jìn)入到command頁(yè)面,這時(shí)候,我點(diǎn)擊返回,它又會(huì)回到登錄頁(yè)面怎么讓它不回去?

clipboard.png

clipboard.png

回答
編輯回答
抱緊我

1:需要登錄頁(yè)面的攔截

/*
* 在beforeEach中攔截
* https://router.vuejs.org/zh-cn/advanced/navigation-guards.html
*/

routes.beforeEach((to, from, next) => {
  if(to.matched.some(record => record.meta.requiresAuth)){
    
    if (noLogin) { // 沒(méi)有登錄則跳轉(zhuǎn)/login頁(yè),進(jìn)行登錄 
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    } else {
      next()
    }
  }else{
    next()
  }
})

2:登錄成功后進(jìn)入到command頁(yè)面,返回不回到登錄頁(yè)

// 登錄成功后的跳轉(zhuǎn)使用router.replace,而不是router.push
this.$router.replace('/command')
2018年9月10日 13:51
編輯回答
解夏

router有個(gè)叫做導(dǎo)航守衛(wèi)的東東

router.beforeEach(()=>{})

具體做法就是在這里面,使用狀態(tài)管理機(jī)對(duì)登錄狀態(tài)做一個(gè)有效的管理,已登錄狀態(tài)下的路由變更,可以直接跳轉(zhuǎn)默認(rèn)的登錄成功后的頁(yè)面。
以上。

2017年10月23日 19:22
編輯回答
命于你

如樓上所說(shuō),跳轉(zhuǎn)到首頁(yè)不要用pushreplace

2017年3月12日 03:02
編輯回答
眼雜

登錄成功后用.replace()

2017年2月15日 03:10
編輯回答
礙你眼

登錄頁(yè)成功后你是如何跳轉(zhuǎn)的。
這樣就不會(huì)出現(xiàn)你的問(wèn)題啊

this.$router.go(-1)
2017年12月5日 01:26