如下所示:
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(); } });
在這之前是給路由加一個meta屬性:
{ path: '/index', meta: { title: '', requireAuth: true, // 添加該字段,表示進入這個路由是需要登錄的 }, }
注意:但是事實是登錄的時候大多數(shù)時候并不進行跳轉(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)到登錄頁面的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com