{ if (to.matched.some(record => record.meta.requireAuth)){ // 判斷該路由是否需要登錄權(quán)限 if (toke..."/>
溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Vue-router路由判斷頁面未登錄跳轉(zhuǎn)到登錄頁面的實(shí)例

發(fā)布時(shí)間:2020-10-16 19:05:57 來源:腳本之家 閱讀:168 作者:蓓蕾心晴 欄目:web開發(fā)

如下所示:

router.beforeEach((to, from, next) => {
 if (to.matched.some(record => record.meta.requireAuth)){ // 判斷該路由是否需要登錄權(quán)限
 if (token) { // 判斷當(dāng)前的token是否存在
  next();
 }
 else {
  next({
  path: '/login',
  query: {redirect: to.fullPath} // 將跳轉(zhuǎn)的路由path作為參數(shù),登錄成功后跳轉(zhuǎn)到該路由
  })
 }
 }
 else {
 next();
 }
});

在這之前是給路由加一個(gè)meta屬性:

{
 path: '/index',
 meta: {
  title: '',
  requireAuth: true, // 添加該字段,表示進(jìn)入這個(gè)路由是需要登錄的
 },
}

注意:但是事實(shí)是登錄的時(shí)候大多數(shù)時(shí)候并不進(jìn)行跳轉(zhuǎn),所以這里需要在login跳轉(zhuǎn)的路徑中再加一段:

if(this.$route.query.redirect){
//  let redirect = decodeURIComponent(this.$route.query.redirect);
  let redirect = this.$route.query.redirect;
  this.$router.push(redirect);
}else{
  this.$router.push('/');
 }

以上這篇Vue-router路由判斷頁面未登錄跳轉(zhuǎn)到登錄頁面的實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI