国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Vue Router activated deactivated 路由守衛(wèi)

這篇具有很好參考價值的文章主要介紹了Vue Router activated deactivated 路由守衛(wèi)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

6.12.activated deactivated

activateddeactivated是路由組件所獨有的兩個鉤子,用于捕獲路由組件的激活狀態(tài)具體使用

  1. activated路由組件被激活時觸發(fā)
  2. deactivated路由組件失活時觸發(fā)

Vue Router activated deactivated 路由守衛(wèi)

src/pages/News.vue

<template>
    <ul>
        <li :style="{opacity}">歡迎學習vue</li>
        <li>news001 <input type="text"></li></li>
        <li>news002 <input type="text"></li></li>
        <li>news003 <input type="text"></li></li>
    </ul>
</template>

<script>
    export default {
        name: "News",
        data(){
            return {
                opacity:1
            }
        },
        activated() {
            console.log('News組件被激活了');
            this.timer = setInterval(() => {
                this.opacity -= 0.01;
                if(this.opacity <= 0) this.opacity = 1
            },16)
        },
        deactivated() {
            console.log('News組件失活了');
            clearInterval(this.timer)
        }
    }
</script>

6.13.路由守衛(wèi)

作用:對路由進行權限控制

分類:全局守衛(wèi)、獨享守衛(wèi)、組件內守衛(wèi) 全局守衛(wèi)

  1. 全局守衛(wèi)
    1. meta路由源信息
      // 全局前置守衛(wèi):初始化時、每次路由切換前執(zhí)行
      router.beforeEach((to,from,next) => {
        console.log('beforeEach',to,from);
        if(to.meta.isAuth){ // 判斷當前路由是否需要進行權限控制
          if(localStorage.getItem('school') === 'atguigu'){ // 權限控制的具體規(guī)則
            next(); // 放行
          } else { 
            alert('暫無權限查看');
          }
        } else { 
          next(); // 放行 
        }
      })
      
      // 全局后置守衛(wèi):初始化時、每次路由切換后執(zhí)行
      router.afterEach((to,from) => {
        console.log('afterEach',to,from);
        if(to.meta.title) { 
          document.title = to.meta.title;//修改網頁的title 
          } else { 
            document.title = 'vue_test' ;
          }
      })
      
  2. 獨享守衛(wèi)
    beforeEnter(to,from,next){
      console.log('beforeEnter',to,from);
      if(localStorage.getItem('school') === 'atguigu') { 
        next();
      } else { 
        alert('暫無權限查看');
      }
    })
    
  3. 組件內守衛(wèi)
    //進入守衛(wèi):通過路由規(guī)則,進入該組件時被調用 
    beforeRouteEnter (to, from, next) {... next()},
    
    //離開守衛(wèi):通過路由規(guī)則,離開該組件時被調用 
    beforeRouteLeave (to, from, next) {... next()},
    

6.13.1.全局路由守衛(wèi)

src/router/index.js

import VueRouter from 'vue-router';

// 引入組件
import About from '../pages/About';
import Home from '../pages/Home';
import News from '../pages/News';
import Message from '../pages/Message';
import Detail from '../pages/Detail';

// 創(chuàng)建并暴露一個路由器
const router =  new VueRouter({
    routes:[
        {
            name:'guanyu',
            path:'/about',
            component:About,
            meta:{title:'關于'}
        },
        {
            name:'zhuye',
            path:'/home',
            component:Home,
            meta:{title:'主頁'},
            children: [
                {
                    name:'xinwen',
                    path:'news',
                    component:News,
                    meta:{isAuth:true,title:'新聞'}
                },
                {
                    name:'xiaoxi',
                    path:'message',
                    component:Message,
                    meta:{isAuth:true,title:'消息'},
                    children: [
                        {
                            name:'Detail', // name配置項為路由命名
                            path:'detail', // 使用占位符聲明接收params參數
                            component:Detail,
                            meta:{isAuth:true,title:'詳情'},
                            props($route){ // 這里可以使用解構賦值
                                return {
                                    id: $route.params.id,
                                    title: $route.params.title
                                }
                            }
                        }
                    ]
                }
            ]
        }
    ]
})

// 全局前置路由守衛(wèi)————初始化的時候、每次路由切換之前被調用
router.beforeEach((to,from,next) => {
    console.log('前置路由守衛(wèi)',to,from);
    if(to.meta.isAuth){
        if(localStorage.getItem('school')==='atguigu'){
            next();
        } else{
            alert('學校名不對,無權限查看!');
        }
    } else {
        next();
    }
});

// 全局后置路由守衛(wèi)————初始化的時候被調用、每次路由切換之后被調用
router.afterEach((to,from) => {
    console.log('后置路由守衛(wèi)',to,from);
    document.title = to.meta.title || '硅谷系統(tǒng)';
});


export  default router;

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

src/router/index.js

import VueRouter from 'vue-router';

// 引入組件
import About from '../pages/About';
import Home from '../pages/Home';
import News from '../pages/News';
import Message from '../pages/Message';
import Detail from '../pages/Detail';

// 創(chuàng)建并暴露一個路由器
const router =  new VueRouter({
    routes:[
        {
            name:'guanyu',
            path:'/about',
            component:About,
            meta:{title:'關于'}
        },
        {
            name:'zhuye',
            path:'/home',
            component:Home,
            meta:{title:'主頁'},
            children: [
                {
                    name:'xinwen',
                    path:'news',
                    component:News,
                    meta:{isAuth:true,title:'新聞'},
                    // ??獨享守衛(wèi),特定路由切換之后被調用
                    beforeEnter(to,from,next){
                        console.log('前置路由守衛(wèi)',to,from);
                        if(to.meta.isAuth){
                            if(localStorage.getItem('school')==='atguigu'){
                                next();
                            } else{
                                alert('學校名不對,無權限查看!');
                            }
                        } else {
                            next();
                        }
                    }
                },
                {
                    name:'xiaoxi',
                    path:'message',
                    component:Message,
                    meta:{isAuth:true,title:'消息'},
                    children: [
                        {
                            name:'Detail', // name配置項為路由命名
                            path:'detail', // 使用占位符聲明接收params參數
                            component:Detail,
                            meta:{isAuth:true,title:'詳情'},
                            props($route){ // 這里可以使用解構賦值
                                return {
                                    id: $route.params.id,
                                    title: $route.params.title
                                }
                            }
                        }
                    ]
                }
            ]
        }
    ]
})

// 全局前置路由守衛(wèi)————初始化的時候、每次路由切換之前被調用
// router.beforeEach((to,from,next) => {
//     console.log('前置路由守衛(wèi)',to,from);
//     if(to.meta.isAuth){
//         if(localStorage.getItem('school')==='atguigu'){
//             next();
//         } else{
//             alert('學校名不對,無權限查看!');
//         }
//     } else {
//         next();
//     }
// });

// 全局后置路由守衛(wèi)————初始化的時候被調用、每次路由切換之后被調用
// router.afterEach((to,from) => {
//     console.log('后置路由守衛(wèi)',to,from);
//     document.title = to.meta.title || '硅谷系統(tǒng)';
// });


export  default router;

6.13.3.組件內路由守衛(wèi)

src/pages/About.vue文章來源地址http://www.zghlxwxcb.cn/news/detail-515674.html

<template>
    <h2>我是About的內容</h2>
</template>

<script>
    export default {
        name: "About",
        // 通過路由規(guī)則,進入該組件時被調用
        beforeRouteEnter (to, from, next) {
            console.log('About--beforeRouteEnter', to, from);
            if(localStorage.getItem('school') === 'atguigu') {
                next();
            } else {
                alert('學校名不對,無權限查看!');
            }
        },
        // 通過路由規(guī)則,離開該組件時被調用
        beforeRouteLeave (to, from, next) {
            console.log('About--beforeRouteLeave', to, from) ;
            next();
        }
    }
</script>

6.14.路由器的兩種工作模式

  1. 對于一個url來說,什么是hash值? #及其后面的內容就是hash值
  2. hash值不會包含在HTTP請求中,即:hash值不會帶給服務器
  3. hash模式
    1. 地址中永遠帶著#號,不美觀
    2. 若以后將地址通過第三方手機app分享,若app校驗嚴格,則地址會被標記為不合法
    3. 兼容性較好
  4. history模式
    1. 地址干凈,美觀
    2. 兼容性和hash模式相比略差
    3. 應用部署上線時需要后端人員支持,解決刷新頁面服務端404的問題
const router = new VueRouter({ 
  mode:'history', 
  routes:[...] 
})

export default router;

到了這里,關于Vue Router activated deactivated 路由守衛(wèi)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!

本文來自互聯(lián)網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • vue中的activated和deactivated

    當頁面被keep-alive緩存下來的時候,vue提供兩個鉤子函數 activated 被 keep-alive 緩存的組件激活時調用。 deactivated 被 keep-alive 緩存的組件失活時調用。 當keepalive頁面緩存,有activated鉤子和created鉤子函數時 這兩個函數會被同時觸發(fā),此時應該使用activated代替created,因為created只會

    2024年02月12日
    瀏覽(11)
  • 路由前置守衛(wèi)router.beforeEach相關用法

    router.beforeEach 是 Vue Router 提供的全局前置守衛(wèi),用于在路由切換之前執(zhí)行一些邏輯。該守衛(wèi)接收三個參數: to : 即將要進入的目標路由對象。 from : 當前導航正要離開的路由。 next : 一個函數,用于 resolve 鉤子。調用 next 表示路由可以繼續(xù)執(zhí)行。 基本用法如下: 在 beforeEach 鉤

    2024年01月21日
    瀏覽(26)
  • Vue 路由 路由守衛(wèi)

    Vue 路由 路由守衛(wèi)

    路由守衛(wèi) 正如其名, vue-router 提供的導航守衛(wèi)主要用來通過跳轉或取消的方式守衛(wèi)導航。簡單來說,就是在路由跳轉 時候的一些鉤子,當從一個頁面跳轉到另一個頁面時,可以在跳轉前、中、后做一些事情。 當你打開一個頁面的前后需要去干什么。 每個守衛(wèi)方法接收參數:

    2024年02月14日
    瀏覽(21)
  • Vue Router active-class 屬性

    active-class?是 vue-router 模塊的? router-link ?組件的屬性,當? router-link ?標簽被點擊時將會應用這個樣式。 單獨在? router-link ?標簽上使用? active-class ?屬性 最后,為您選中的鏈接設置樣式: 點擊后,無論怎么刷新頁面,樣式效果都不會去掉,除非跳轉到其他頁面。。(這一

    2024年02月11日
    瀏覽(12)
  • vue - 路由守衛(wèi)

    Vue路由守衛(wèi)是一種機制,用于在導航過程中對路由進行監(jiān)控和控制。Vue?Router提供了三種類型的路由守衛(wèi): 1.?全局前置守衛(wèi):router.beforeEach 2.?全局解析守衛(wèi):router.beforeResolve 3.?全局后置守衛(wèi):router.afterEach 這些守衛(wèi)可以用來進行權限驗證、頁面加載前的處理、頁面切換時的

    2024年04月25日
    瀏覽(25)
  • vue三種路由守衛(wèi)詳解

    vue三種路由守衛(wèi)詳解

    查看本專欄目錄 關于作者 還是大劍師蘭特 :曾是美國某知名大學計算機專業(yè)研究生,現(xiàn)為航空航海領域高級前端工程師;CSDN知名博主,GIS領域優(yōu)質創(chuàng)作者,深耕openlayers、leaflet、mapbox、cesium,canvas,webgl,echarts等技術開發(fā),歡迎加底部微信,一起交流。 熱門推薦 內容鏈接

    2024年02月20日
    瀏覽(23)
  • 解密!Vue路由守衛(wèi)的使用

    解密!Vue路由守衛(wèi)的使用

    什么是路由 對路由這個概念,可能有些人并不很理解,但是路由器大家應該都不陌生。我們可以先來了解一下路由器是什么。路由器現(xiàn)在基本上家家都會有,大家是否好奇過它名字的來源?其實搞懂了路由器名字的來歷,我們就可以搞懂什么是路由了,如下圖: 上面兩張圖分

    2024年02月11日
    瀏覽(42)
  • React/Vue實現(xiàn)路由鑒權/導航守衛(wèi)/路由攔截

    1、實現(xiàn)思路 自己封裝? AuthRoute ?路由鑒權高階組件,實現(xiàn)未登錄攔截,并跳轉到登錄頁面 思路為:判斷本地是否有token,如果有,就返回子組件,否則就重定向到登錄Login 2、實現(xiàn)步驟 在 components 目錄中,創(chuàng)建 AuthRoute/index.js 文件 判斷是否登錄 登錄時,直接渲染相應頁面組

    2024年02月15日
    瀏覽(25)
  • 編程式導航、緩存路由組件、路由守衛(wèi)、Vue UI組件庫【VUE】

    作用:不借助 router-link 實現(xiàn)路由跳轉,讓路由跳轉更加靈活 具體編碼: 作用:讓不展示的路由組件保持掛載,不被銷毀 具體編碼: 作用:路由組件所獨有的兩個鉤子,用于捕獲路由組件的激活狀態(tài)。 具體名字: (1)activated路由組件被激活時觸發(fā) (2)deactivated路由組件失

    2024年02月03日
    瀏覽(53)
  • 【學Vue就跟玩一樣】Vue中的路由與多種守衛(wèi)

    【學Vue就跟玩一樣】Vue中的路由與多種守衛(wèi)

    vue的一個插件庫,專門用來實現(xiàn)對 SPA應用 的單頁Web應用(single page web application, SPA)。整個應用只有一個完整的頁面。點擊頁面中的導航鏈接不會刷新頁面,只會做頁面的局部更新。數據需要通過ajax請求獲取。 一個路由就是一組映射關系(key :value) key 為路徑, value可能是funct

    2024年02月02日
    瀏覽(57)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包