溫馨提示×

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

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

vue下怎么使用全局路由守衛(wèi)

發(fā)布時(shí)間:2022-10-18 11:11:34 來源:億速云 閱讀:152 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了vue下怎么使用全局路由守衛(wèi)的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇vue下怎么使用全局路由守衛(wèi)文章都會(huì)有所收獲,下面我們一起來看看吧。

1.首先,新建一個(gè)html項(xiàng)目,并在項(xiàng)目中安裝vue-router;

npm install vue-router


2.vue-router安裝好,在項(xiàng)目中引入vue和vue-router;

import Vue from 'vue'

import Router from 'vue-router'


3.vue和vue-router引入后,在項(xiàng)目中使用Vue.use方法注冊(cè)一個(gè)路由;

Vue.use(VueRouter)


4.路由注冊(cè)好后,在項(xiàng)目中定義路由并配置;

export default new vueRouter({

routes: [{

path: '/',

component: Index

}, {

path: '/Detail',

component: Detail,

meta: {

requireLogin: 2

}

}, {

path: '/Center',

component: Center,

meta: {

requireLogin: 1

}

}]

})


5.最后,路由配置好后,在項(xiàng)目中執(zhí)行以下代碼即可使用全局路由守衛(wèi);

import router from './router'


router.beforeEach((to, from, next) => {


let token = window.localStorage.getItem('token')

if (to.meta.requireLogin && to.meta.requireLogin === 1) {

if (token) {

next()

} else {

next('/')

}

} else if (to.meta.requireLogin && to.meta.requireLogin === 2) {

if (token) {

next('/center')

} else {

next()

}

} else {

next()

}

})


關(guān)于“vue下怎么使用全局路由守衛(wèi)”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“vue下怎么使用全局路由守衛(wèi)”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

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

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

vue
AI