Vue守衛(wèi)零基礎(chǔ)介紹
1. 全局導(dǎo)航守衛(wèi)
語(yǔ)法:
# 守衛(wèi)參數(shù)
+ to: Route: 即將要進(jìn)入的目標(biāo) 路由對(duì)象
+ from: Route: 當(dāng)前導(dǎo)航正要離開(kāi)的路由
+ next: Function: 一定要調(diào)用該next方法,否則路由不向下執(zhí)行,頁(yè)面空白。# 全局前置守衛(wèi),當(dāng)一個(gè)導(dǎo)航觸發(fā)時(shí),立刻觸發(fā)前置守衛(wèi),
router.beforeEach((to, from, next) => {
// ...
next()
})//全局解析守衛(wèi),等到路由獨(dú)享守衛(wèi)和組件內(nèi)守衛(wèi)都解析完畢后執(zhí)行
router.beforeResolve((to, from, next) => {
// ...
next()
})# 全局后置鉤子,全部守衛(wèi)執(zhí)行完畢后執(zhí)行
// 此鉤子不會(huì)接受 next 函數(shù)也不會(huì)改變導(dǎo)航本身
router.afterEach((to, from) => {
// ...
})
全局導(dǎo)航守衛(wèi)執(zhí)行順序:
news.js(這個(gè)文件是從 index.js 文件中抽取拆分出來(lái)的,最終要被引入到 insex.js 文件中):
import News from '@/views/News' import Detail from '@/views/Detail' import Login from '@/views/Login' const routes = [ { path: '/news', component: News, }, { path: '/news/:id', name: 'xw', component: Detail, }, { // 這是登錄頁(yè) path: '/login', component: Login, } ] export default routes
index.js:
import Vue from 'vue' import VueRouter from 'vue-router' import news from './routes/news' // 以插件的方式添加 Vue.use(VueRouter) // 實(shí)例化路由對(duì)象及配置路由表 const routes = [...news] const router = new VueRouter({ // 路由模式 mode: 'history', // 路由規(guī)則表 routes }) // 全局守衛(wèi) 每次切換頁(yè)面都會(huì)執(zhí)行到 // 前置 router.beforeEach((to, from, next) => { console.log('全局 --- beforeEach') next() }) // 解析 router.beforeResolve((to, from, next) => { console.log('全局 --- beforeResolve') next() }) // 后置 router.afterEach((to, from) => { console.log('全局 --- afterEach') }) export default router
登錄頁(yè)(index.vue):
<template> <div> <button>登錄用戶</button> </div> </template> <script> export default { } </script> <style lang="scss" scoped></style>
現(xiàn)在我們有這樣一個(gè)需求,用戶只有在登錄成功之后,才能訪問(wèn)新聞頁(yè)面,該怎么做呢?
index.js:
import Vue from 'vue' import VueRouter from 'vue-router' import news from './routes/news' // 以插件的方式添加 Vue.use(VueRouter) // 實(shí)例化路由對(duì)象及配置路由表 const routes = [...news] const router = new VueRouter({ // 路由模式 mode: 'history', // 路由規(guī)則表 routes }) // 用全局前置守衛(wèi)判斷用戶是否登錄 router.beforeEach((to, from, next) => { // 在使用導(dǎo)航守衛(wèi)來(lái)驗(yàn)證用戶是否登錄,一定要把登錄頁(yè)面路由排除掉,防止死循環(huán) // 如果沒(méi)有在本地存儲(chǔ)中獲取到token值,并且即將跳轉(zhuǎn)的頁(yè)面不是登錄頁(yè) if (!sessionStorage.getItem('token') && to.path != '/login') { // 到登錄頁(yè)面 // next('/login') // replace: true表示跳轉(zhuǎn)到登錄頁(yè)面后,不允許回退 next({ path: '/login', replace: true }) } else { next() } }) export default router
2. 路由獨(dú)享守衛(wèi)
語(yǔ)法:
const router = new VueRouter({ routes: [ { path: '/foo', component: Foo, beforeEnter: (to, from, next) => { // ... next() } } ] })
使用:
news.js(這個(gè)文件是從 index.js 文件中抽取拆分出來(lái)的,最終要被引入到 insex.js 文件中):
import News from '@/views/News' import Detail from '@/views/Detail' import Login from '@/views/Login' const routes = [ { path: '/news', component: News, }, { path: '/news/:id', name: 'xw', component: Detail, }, { // 這是登錄頁(yè) path: '/login', component: Login, // 路由獨(dú)享守衛(wèi) // 只有當(dāng)前的路由規(guī)則才生效,比如登錄頁(yè)面的路由獨(dú)享守衛(wèi)在進(jìn)入新聞頁(yè)面時(shí)就不會(huì)生效 // 路由獨(dú)享守衛(wèi)在每次進(jìn)入到當(dāng)前路由頁(yè)面時(shí)都會(huì)執(zhí)行 beforeEnter: (to, from, next) => { console.log('路由獨(dú)享守衛(wèi) ==login -- --- beforeEnter') next() } } ] export default routes
3. 組件內(nèi)守衛(wèi)
語(yǔ)法:
你可以在路由組件內(nèi)直接定義以下路由導(dǎo)航守衛(wèi):
const Foo = { template: `...`, //執(zhí)行完全局前置守衛(wèi)和路由獨(dú)享守衛(wèi),就會(huì)執(zhí)行當(dāng)前函數(shù) beforeRouteEnter (to, from, next) { // 在渲染該組件的對(duì)應(yīng)路由被 confirm 前調(diào)用 // 不!能!獲取組件實(shí)例 `this` // 因?yàn)楫?dāng)守衛(wèi)執(zhí)行前,組件實(shí)例還沒(méi)被創(chuàng)建 }, //動(dòng)態(tài)路由參數(shù)改變就會(huì)觸發(fā)這個(gè)函數(shù) beforeRouteUpdate (to, from, next) { // 在當(dāng)前路由改變,但是該組件被復(fù)用時(shí)調(diào)用 // 舉例來(lái)說(shuō),對(duì)于一個(gè)帶有動(dòng)態(tài)參數(shù)的路徑 /foo/:id,在 /foo/1 和 /foo/2 之間跳轉(zhuǎn)的時(shí)候, // 由于會(huì)渲染同樣的 Foo 組件,因此組件實(shí)例會(huì)被復(fù)用。而這個(gè)鉤子就會(huì)在這個(gè)情況下被調(diào)用。 // 可以訪問(wèn)組件實(shí)例 `this` }, //離開(kāi)當(dāng)前頁(yè)面時(shí)調(diào)用 beforeRouteLeave (to, from, next) { // 導(dǎo)航離開(kāi)該組件的對(duì)應(yīng)路由時(shí)調(diào)用 // 可以訪問(wèn)組件實(shí)例 `this` } }
所有守衛(wèi)和生命周期函數(shù)的執(zhí)行順序:
news.js(這個(gè)文件是從 index.js 文件中抽取拆分出來(lái)的,最終要被引入到 insex.js 文件中):
import News from '@/views/News' import Detail from '@/views/Detail' import Login from '@/views/Login' const routes = [ { path: '/news', component: News, }, { path: '/news/:id', name: 'xw', component: Detail, beforeEnter: (to, from, next) => { console.log('路由獨(dú)享守衛(wèi) -- detail --- beforeEnter') next() } }, { // 這是登錄頁(yè) path: '/login', component: Login, // 路由獨(dú)享守衛(wèi) // 只有當(dāng)前的路由規(guī)則才生效,比如登錄頁(yè)面的路由獨(dú)享守衛(wèi)在進(jìn)入新聞頁(yè)面時(shí)就不會(huì)生效 // 路由獨(dú)享守衛(wèi)在每次進(jìn)入到當(dāng)前路由頁(yè)面時(shí)都會(huì)執(zhí)行 beforeEnter: (to, from, next) => { console.log('路由獨(dú)享守衛(wèi) ==login -- --- beforeEnter') next() }, } ] export default routes
詳情頁(yè)(index.vue):
<template> <div> <h3>新聞詳情頁(yè)</h3> </div> </template> <script> export default { // 當(dāng)路由訪問(wèn)到此組件時(shí),執(zhí)行此鉤子函數(shù) beforeRouteEnter(to, from, next) { console.log("組件 --- beforeRouteEnter"); next(); }, // 離開(kāi)當(dāng)前路由組件 beforeRouteLeave(to, from, next) { console.log("組件 --- beforeRouteLeave"); next(); }, // 路由參數(shù)的改變,觸發(fā)路由組件守衛(wèi) beforeRouteUpdate(to, from, next) { console.log(this); console.log("組件 --- beforeRouteUpdate"); next(); }, // 和生命周期函數(shù)比較以下執(zhí)行順序 // 所有路由解析完畢以后,才開(kāi)始執(zhí)行生命周期函數(shù) beforeCreate() { console.log('組件 === beforeCreate') }, beforeDestroy() { console.log('組件 === beforeDestroy') }, destroyed() { console.log('組件 === destroyed') }, }; </script> <style lang="scss" scoped></style>
下面我們來(lái)看beforeRouteUpdate
函數(shù)什么時(shí)候執(zhí)行。
詳情頁(yè)(index.vue):
<template> <div> <h3>新聞詳情頁(yè)</h3> <router-link to="/news/1">111</router-link><br /> <router-link to="/news/2">222</router-link><br /> <router-link to="/news/3">333</router-link> </div> </template> <script> export default { // 當(dāng)路由訪問(wèn)到此組件時(shí),執(zhí)行此鉤子函數(shù) beforeRouteEnter(to, from, next) { console.log("組件 --- beforeRouteEnter"); next(); }, // 離開(kāi)當(dāng)前路由組件 beforeRouteLeave(to, from, next) { console.log("組件 --- beforeRouteLeave"); next(); }; // 路由參數(shù)的改變,觸發(fā)路由組件守衛(wèi) // 可以用來(lái)監(jiān)聽(tīng)頁(yè)面是否發(fā)生變化 beforeRouteUpdate(to, from, next) { // console.log(this); console.log("組件 --- beforeRouteUpdate"); next(); }, // 監(jiān)聽(tīng)器也可以用來(lái)監(jiān)聽(tīng)頁(yè)面是否發(fā)生變化 // watch:{ // '$route'(n){ // console.log('watch --- ' ,n); // } // }, // 和生命周期函數(shù)比較以下執(zhí)行順序 // 所有路由解析完畢以后,才開(kāi)始執(zhí)行生命周期函數(shù) beforeCreate() { console.log('組件 === beforeCreate') }, beforeDestroy() { console.log('組件 === beforeDestroy') }, destroyed() { console.log('組件 === destroyed') }, }; </script> <style lang="scss" scoped></style>
到此這篇關(guān)于Vue守衛(wèi)零基礎(chǔ)介紹的文章就介紹到這了,更多相關(guān)Vue守衛(wèi)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 快速理解Vue路由導(dǎo)航守衛(wèi)
- Vue2.x配置路由導(dǎo)航守衛(wèi)實(shí)現(xiàn)用戶登錄和退出
- vue 路由守衛(wèi)(導(dǎo)航守衛(wèi))及其具體使用
- vue路由導(dǎo)航守衛(wèi)和請(qǐng)求攔截以及基于node的token認(rèn)證的方法
- 關(guān)于Vue Router中路由守衛(wèi)的應(yīng)用及在全局導(dǎo)航守衛(wèi)中檢查元字段的方法
- 詳解Vue的鉤子函數(shù)(路由導(dǎo)航守衛(wèi)、keep-alive、生命周期鉤子)
- vue2.0 實(shí)現(xiàn)導(dǎo)航守衛(wèi)(路由守衛(wèi))
- vue2.0 實(shí)現(xiàn)導(dǎo)航守衛(wèi)的具體用法(路由守衛(wèi))
- Vue的路由動(dòng)態(tài)重定向和導(dǎo)航守衛(wèi)實(shí)例
相關(guān)文章
實(shí)現(xiàn)一個(gè) Vue 吸頂錨點(diǎn)組件方法
這篇文章主要介紹了實(shí)現(xiàn)一個(gè) Vue 吸頂錨點(diǎn)組件方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Vue.js第二天學(xué)習(xí)筆記(vue-router)
這篇文章主要為大家詳細(xì)介紹了Vue.js第二天的學(xué)習(xí)筆記,關(guān)于vue-router的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Vue中@click事件的常見(jiàn)修飾符用法總結(jié)
這篇文章主要給大家介紹了關(guān)于Vue中@click事件的常見(jiàn)修飾符用法的相關(guān)資料,@click事件修飾符是在Vue組件中用來(lái)修改@click事件行為的特殊標(biāo)記,需要的朋友可以參考下2023-10-10vue使用wavesurfer.js解決音頻可視化播放問(wèn)題
Wavesurfer.js是一款基于HTML5?canvas和Web?Audio的聲紋可視化插件,功能十分強(qiáng)大,在Vue框架中嵌入使用該插件,今天重點(diǎn)給大家介紹下vue使用wavesurfer.js解決音頻可視化播放問(wèn)題,感興趣的朋友一起看看吧2022-04-04VSCode Vue開(kāi)發(fā)推薦插件和VSCode快捷鍵(小結(jié))
這篇文章主要介紹了VSCode Vue開(kāi)發(fā)推薦插件和VSCode快捷鍵(小結(jié)),文中通過(guò)圖文表格介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08