久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

Vue使用路由鉤子攔截器beforeEach和afterEach監聽路由

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

在路由跳轉的時候,我們需要一些權限判斷或者其他操作。這個時候就需要使用路由的鉤子函數。

定義:路由鉤子主要是給使用者在路由發生變化時進行一些特殊的處理而定義的函數。

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

兩種函數:

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

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

全局鉤子函數

顧名思義,它是對全局有效的一個函數

// 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即將進入的路由對象 // console.log('from:',from); // 當前導航即將離開的路由 // console.log('next:',next); // Function,進行管道中的一個鉤子,如果執行完了,則導航的狀態就是 confirmed (確認的);否則為false,終止導航。 if(to.name==’notSend’){ next({ name:’sort’ }) return } next()})export default router

某個路由的鉤子函數

顧名思義,它是寫在某個路由里頭的函數,本質上跟組件內函數沒有區別。

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

路由組件的鉤子

可以在路由組件內直接定義以下路由導航鉤子

// *.vuebeforeRouteEnterbeforeRouteUpdate (2.2 新增)beforeRouteLeave

這里簡單說下鉤子函數的用法:它是和data,methods平級的。

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Vue
相關文章:
主站蜘蛛池模板: 龙游县| 邢台县| 东兰县| 遂溪县| 通江县| 财经| 灌云县| 巨鹿县| 新疆| 阜新| 汽车| 娄底市| 襄垣县| 梁河县| 湖州市| 彭水| 临湘市| 拜泉县| 个旧市| 桐城市| 益阳市| 华亭县| 河津市| 伊金霍洛旗| 通海县| 屏东县| 武强县| 肥西县| 江山市| 页游| 迁西县| 五家渠市| 宿迁市| 阿巴嘎旗| 仙游县| 宁陕县| 万山特区| 宜宾市| 嫩江县| 临夏县| 呼伦贝尔市|