最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue守衛(wèi)零基礎介紹

 更新時間:2022年09月30日 16:20:19   作者:月光曬了很涼快  
導航守衛(wèi)就是路由跳轉過程中的一些鉤子函數(shù),這個過程中觸發(fā)的這些函數(shù)能讓你有操作一些其他的事兒的時機,這就是導航守衛(wèi),守衛(wèi)適用于切面編程,即把一件事切成好幾塊,然后分給不同的人去完成,提高工作效率,也可以讓代碼變得更加優(yōu)雅

1. 全局導航守衛(wèi)

語法:

# 守衛(wèi)參數(shù)
    + to: Route: 即將要進入的目標 路由對象
    + from: Route: 當前導航正要離開的路由
    + next: Function: 一定要調(diào)用該next方法,否則路由不向下執(zhí)行,頁面空白。

# 全局前置守衛(wèi),當一個導航觸發(fā)時,立刻觸發(fā)前置守衛(wèi),
router.beforeEach((to, from, next) => {
  // ...
  next()
})

//全局解析守衛(wèi),等到路由獨享守衛(wèi)和組件內(nèi)守衛(wèi)都解析完畢后執(zhí)行
router.beforeResolve((to, from, next) => {
  // ...
  next()
})

# 全局后置鉤子,全部守衛(wèi)執(zhí)行完畢后執(zhí)行
// 此鉤子不會接受 next 函數(shù)也不會改變導航本身
router.afterEach((to, from) => {
  // ...
})

全局導航守衛(wèi)執(zhí)行順序:

news.js(這個文件是從 index.js 文件中抽取拆分出來的,最終要被引入到 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,
    },
    {
        // 這是登錄頁
        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)
// 實例化路由對象及配置路由表
const routes = [...news]
const router = new VueRouter({
  // 路由模式
  mode: 'history',
  // 路由規(guī)則表
  routes
})
// 全局守衛(wèi) 每次切換頁面都會執(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

登錄頁(index.vue):

<template>
  <div>
    <button>登錄用戶</button>
  </div>
</template>
<script>
export default {
}
</script>
<style lang="scss" scoped></style>

現(xiàn)在我們有這樣一個需求,用戶只有在登錄成功之后,才能訪問新聞頁面,該怎么做呢?

index.js:

import Vue from 'vue'
import VueRouter from 'vue-router'
import news from './routes/news'
// 以插件的方式添加
Vue.use(VueRouter)
// 實例化路由對象及配置路由表
const routes = [...news]
const router = new VueRouter({
  // 路由模式
  mode: 'history',
  // 路由規(guī)則表
  routes
})
// 用全局前置守衛(wèi)判斷用戶是否登錄
router.beforeEach((to, from, next) => {
  // 在使用導航守衛(wèi)來驗證用戶是否登錄,一定要把登錄頁面路由排除掉,防止死循環(huán)
  // 如果沒有在本地存儲中獲取到token值,并且即將跳轉的頁面不是登錄頁
  if (!sessionStorage.getItem('token') && to.path != '/login') {
    // 到登錄頁面
    // next('/login')
    // replace: true表示跳轉到登錄頁面后,不允許回退
    next({ path: '/login', replace: true })
  } else {
    next()
  }
})
export default router

2. 路由獨享守衛(wèi)

語法:

const router = new VueRouter({
  routes: [
    {
      path: '/foo',
      component: Foo,
      beforeEnter: (to, from, next) => {
        // ...
		next()
      }
    }
  ]
})

使用:

news.js(這個文件是從 index.js 文件中抽取拆分出來的,最終要被引入到 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,
  },
    {
      // 這是登錄頁
      path: '/login',
      component: Login,
      // 路由獨享守衛(wèi) 
      // 只有當前的路由規(guī)則才生效,比如登錄頁面的路由獨享守衛(wèi)在進入新聞頁面時就不會生效
      // 路由獨享守衛(wèi)在每次進入到當前路由頁面時都會執(zhí)行
      beforeEnter: (to, from, next) => {
        console.log('路由獨享守衛(wèi) ==login -- --- beforeEnter')
        next()
      }
    }
]
export default routes

3. 組件內(nèi)守衛(wèi)

語法:

你可以在路由組件內(nèi)直接定義以下路由導航守衛(wèi):

const Foo = {
  template: `...`,
  //執(zhí)行完全局前置守衛(wèi)和路由獨享守衛(wèi),就會執(zhí)行當前函數(shù)
  beforeRouteEnter (to, from, next) {
    // 在渲染該組件的對應路由被 confirm 前調(diào)用
    // 不!能!獲取組件實例 `this`
    // 因為當守衛(wèi)執(zhí)行前,組件實例還沒被創(chuàng)建
  },
  //動態(tài)路由參數(shù)改變就會觸發(fā)這個函數(shù)
  beforeRouteUpdate (to, from, next) {
    // 在當前路由改變,但是該組件被復用時調(diào)用
    // 舉例來說,對于一個帶有動態(tài)參數(shù)的路徑 /foo/:id,在 /foo/1 和 /foo/2 之間跳轉的時候,
    // 由于會渲染同樣的 Foo 組件,因此組件實例會被復用。而這個鉤子就會在這個情況下被調(diào)用。
    // 可以訪問組件實例 `this`
  },
  //離開當前頁面時調(diào)用
  beforeRouteLeave (to, from, next) {
    // 導航離開該組件的對應路由時調(diào)用
    // 可以訪問組件實例 `this`
  }
}

所有守衛(wèi)和生命周期函數(shù)的執(zhí)行順序:

news.js(這個文件是從 index.js 文件中抽取拆分出來的,最終要被引入到 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('路由獨享守衛(wèi) -- detail --- beforeEnter')
      next()
    }
  },
    {
      // 這是登錄頁
      path: '/login',
      component: Login,
      // 路由獨享守衛(wèi) 
      // 只有當前的路由規(guī)則才生效,比如登錄頁面的路由獨享守衛(wèi)在進入新聞頁面時就不會生效
      // 路由獨享守衛(wèi)在每次進入到當前路由頁面時都會執(zhí)行
      beforeEnter: (to, from, next) => {
        console.log('路由獨享守衛(wèi) ==login -- --- beforeEnter')
        next()
      },
    }
]
export default routes

詳情頁(index.vue):

<template>
  <div>
    <h3>新聞詳情頁</h3>
  </div>
</template>
<script>
export default {
  // 當路由訪問到此組件時,執(zhí)行此鉤子函數(shù)
  beforeRouteEnter(to, from, next) {
    console.log("組件 --- beforeRouteEnter");
    next();
  },
  // 離開當前路由組件
  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í)行順序
  // 所有路由解析完畢以后,才開始執(zhí)行生命周期函數(shù)
  beforeCreate() {
    console.log('組件 === beforeCreate')
  },
  beforeDestroy() {
    console.log('組件 === beforeDestroy')
  },
  destroyed() {
    console.log('組件 === destroyed')
  },
};
</script>
<style lang="scss" scoped></style>

下面我們來看beforeRouteUpdate函數(shù)什么時候執(zhí)行。

詳情頁(index.vue):

<template>
  <div>
    <h3>新聞詳情頁</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 {
  // 當路由訪問到此組件時,執(zhí)行此鉤子函數(shù)
  beforeRouteEnter(to, from, next) {
    console.log("組件 --- beforeRouteEnter");
    next();
  },
  // 離開當前路由組件
  beforeRouteLeave(to, from, next) {
    console.log("組件 --- beforeRouteLeave");
    next();
  };
  // 路由參數(shù)的改變,觸發(fā)路由組件守衛(wèi)
  // 可以用來監(jiān)聽頁面是否發(fā)生變化
  beforeRouteUpdate(to, from, next) {
    // console.log(this);
    console.log("組件 --- beforeRouteUpdate");
    next();
  },
  // 監(jiān)聽器也可以用來監(jiān)聽頁面是否發(fā)生變化
  // watch:{
  //   '$route'(n){
  //     console.log('watch --- ' ,n);
  //   }
  // },
  // 和生命周期函數(shù)比較以下執(zhí)行順序
  // 所有路由解析完畢以后,才開始執(zhí)行生命周期函數(shù)
  beforeCreate() {
    console.log('組件 === beforeCreate')
  },

  beforeDestroy() {
    console.log('組件 === beforeDestroy')
  },
  destroyed() {
    console.log('組件 === destroyed')
  },
};
</script>
<style lang="scss" scoped></style>

到此這篇關于Vue守衛(wèi)零基礎介紹的文章就介紹到這了,更多相關Vue守衛(wèi)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Vue3通過hooks方式封裝節(jié)流和防抖的代碼詳解

    Vue3通過hooks方式封裝節(jié)流和防抖的代碼詳解

    vue3 中的 hooks 就是函數(shù)的一種寫法,就是將文件的一些單獨功能的js代碼進行抽離出來,放到單獨的js文件中,或者說是一些可以復用的公共方法/功能,本文給大家介紹了Vue3通過hooks方式封裝節(jié)流和防抖,需要的朋友可以參考下
    2024-10-10
  • vue遞歸組件實戰(zhàn)之簡單樹形控件實例代碼

    vue遞歸組件實戰(zhàn)之簡單樹形控件實例代碼

    這篇文章主要介紹了vue遞歸組件實戰(zhàn)之簡單樹形控件的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-08-08
  • vue router返回到指定的路由的場景分析

    vue router返回到指定的路由的場景分析

    這篇文章主要介紹了vue router返回到指定的路由的場景分析,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • 使用vue-cli3新建一個項目并寫好基本配置(推薦)

    使用vue-cli3新建一個項目并寫好基本配置(推薦)

    這篇文章主要介紹了使用vue-cli3新建一個項目并寫好基本配置的實例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2019-04-04
  • elementUI select組件value值注意事項詳解

    elementUI select組件value值注意事項詳解

    這篇文章主要介紹了elementUI select組件value值注意事項詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-05-05
  • vue-router如何實時動態(tài)替換路由參數(shù)(地址欄參數(shù))

    vue-router如何實時動態(tài)替換路由參數(shù)(地址欄參數(shù))

    這篇文章主要介紹了vue-router如何實時動態(tài)替換路由參數(shù)(地址欄參數(shù)),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • 詳解vue中axios請求的封裝

    詳解vue中axios請求的封裝

    這篇文章主要介紹了vue中axios請求的封裝,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-04-04
  • vue實現(xiàn)日歷備忘錄功能

    vue實現(xiàn)日歷備忘錄功能

    這篇文章主要為大家詳細介紹了vue實現(xiàn)日歷備忘錄功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • 實現(xiàn)一個Vue版Upload組件

    實現(xiàn)一個Vue版Upload組件

    這篇文章主要介紹了實現(xiàn)一個Vue版Upload組件,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-08-08
  • Vue3實現(xiàn)跑馬燈效果

    Vue3實現(xiàn)跑馬燈效果

    這篇文章主要為大家詳細介紹了Vue3實現(xiàn)跑馬燈效果,可以更換顏色,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04

最新評論

龙口市| 义乌市| 迁安市| 盐池县| 嘉禾县| 榆树市| 三明市| 吉安县| 大余县| 高密市| 南投市| 铁力市| 新和县| 同江市| 容城县| 龙游县| 西青区| 阳曲县| 永德县| 龙陵县| 安多县| 许昌市| 辛集市| 托克逊县| 大英县| 凤山县| 封丘县| 桐庐县| 朝阳区| 蒲城县| 和顺县| 威远县| 曲周县| 盐亭县| 新民市| 景泰县| 武宣县| 明星| 榆社县| 乡城县| 中宁县|