国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Vue使用路由鉤子攔截器beforeEach和afterEach監(jiān)聽路由

瀏覽:43日期:2022-10-27 14:37:33

在路由跳轉(zhuǎn)的時(shí)候,我們需要一些權(quán)限判斷或者其他操作。這個(gè)時(shí)候就需要使用路由的鉤子函數(shù)。

定義:路由鉤子主要是給使用者在路由發(fā)生變化時(shí)進(jìn)行一些特殊的處理而定義的函數(shù)。

總體來講vue里面提供了三大類鉤子,兩種函數(shù) 1、全局鉤子 2、某個(gè)路由的鉤子 3、組件內(nèi)鉤子

兩種函數(shù):

1. router.beforeEach(function(to,form,next){}) /*在跳轉(zhuǎn)之前執(zhí)行*/

2. router.afterEach(function(to,form)}{}) /*在跳轉(zhuǎn)之后判斷*/

全局鉤子函數(shù)

顧名思義,它是對(duì)全局有效的一個(gè)函數(shù)

// router.jsconst router = new Router({ routes: [ { path: ’/’, name: ’sideBar’, component: sideBar, children:[ { path:’/’, name:’sort’, component:sort }, { path:’/addNewSort’, name:’addNewSort’, component:addNewSort }, { path:’/notSend’, name:’notSend’, component:notSend }, { path:’/sended’, name:’sended’, component:sended }, } ]})router.beforeEach((to,from,next)=>{ // console.log('to:',to); // router即將進(jìn)入的路由對(duì)象 // console.log('from:',from); // 當(dāng)前導(dǎo)航即將離開的路由 // console.log('next:',next); // Function,進(jìn)行管道中的一個(gè)鉤子,如果執(zhí)行完了,則導(dǎo)航的狀態(tài)就是 confirmed (確認(rèn)的);否則為false,終止導(dǎo)航。 if(to.name==’notSend’){ next({ name:’sort’ }) return } next()})export default router

某個(gè)路由的鉤子函數(shù)

顧名思義,它是寫在某個(gè)路由里頭的函數(shù),本質(zhì)上跟組件內(nèi)函數(shù)沒有區(qū)別。

// router.jsconst router = new VueRouter({ routes: [ { path: ’/login’, component: Login, beforeEnter: (to, from, next) => { // ... }, beforeLeave: (to, from, next) => { // ... } } ]})

路由組件的鉤子

可以在路由組件內(nèi)直接定義以下路由導(dǎo)航鉤子

// *.vuebeforeRouteEnterbeforeRouteUpdate (2.2 新增)beforeRouteLeave

這里簡(jiǎn)單說下鉤子函數(shù)的用法:它是和data,methods平級(jí)的。

beforeRouteLeave(to, from, next) { next()},beforeRouteEnter(to, from, next) { next()},beforeRouteUpdate(to, from, next) { // 用于相同路由組件的的參數(shù)更新 next()},data:{},method: {}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 剑阁县| 沅陵县| 和顺县| 鄯善县| 连江县| 高淳县| 南平市| 张掖市| 连江县| 东光县| 商洛市| 蒲城县| 平阴县| 本溪市| 科技| 长沙县| 锡林浩特市| 宾阳县| 阜城县| 两当县| 大港区| 龙岩市| 松滋市| 开江县| 民勤县| 青神县| 定边县| 丹寨县| 额敏县| 翁牛特旗| 万安县| 甘孜| 林周县| 清丰县| 新巴尔虎左旗| 茶陵县| 靖州| 明水县| 铜梁县| 定日县| 兴安盟|