{ if (to.matched.some(record => record.meta.requireAuth)){ // 判斷該路由是否需要登錄權(quán)限 if (toke..."/>
您好,登錄后才能下訂單哦!
如下所示:
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è)參考,也希望大家多多支持億速云。
免責(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)容。