日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

局部路由守衛(wèi)component守衛(wèi)

作者:南瓜骨頭 更新時(shí)間: 2023-11-24 編程語言

局部路由守衛(wèi)component守衛(wèi)

component守衛(wèi)(beforeRouteEnter、beforeRouteLeave)

  • 代碼位置:在路由組件中,代碼是寫在component當(dāng)中的(XXX.vue)
  • beforeRouteEnter、beforeRouteLeave都有三個(gè)參數(shù):
    • to參數(shù):to是一個(gè)路由對(duì)象,表示到哪兒去(跳轉(zhuǎn)的下一個(gè)路由組件)。
    • from參數(shù):form是一個(gè)路由對(duì)象,表示從哪來(從哪個(gè)路由切換過來的)。
    • next參數(shù):next是一個(gè)函數(shù),表示允許通過,可以繼續(xù)向下走。
  • beforeRouteEnter格式:beforeRouteEnter(to, from, next){}
  • beforeRouteLeave格式:beforeRouteLeave(to, from, next){}
  • 執(zhí)行時(shí)機(jī):
    • beforeRouteEnter的執(zhí)行時(shí)機(jī):進(jìn)入路由組件之前執(zhí)行
    • beforeRouteLeave的執(zhí)行時(shí)機(jī):離開路由組件之前執(zhí)行
    • 兩個(gè)函數(shù)都是執(zhí)行一個(gè)對(duì)象
  • 全局前置、后置路由守衛(wèi) 和 component守衛(wèi)的區(qū)別
    • 全局前置、后置路由守衛(wèi)在調(diào)用時(shí)兩個(gè)都會(huì)啟動(dòng)
    • beforeRouteEnter、beforeRouteLeave在調(diào)用時(shí)只能有一個(gè)能用,另一個(gè)就不能用

在這里插入圖片描述

// City.vue
<template>
    <div>
        <h2>區(qū)</h2>
        <ul>
            <li>{{a1}}</li>
            <li>{{a2}}</li>
            <li>{{a3}}</li>
        </ul>
    </div>
</template>

<script>
    export default {
        name : 'City',
        props : ['a1', 'a2', 'a3'],
        beforeRouteEnter(to, from, next){
            console.log(`進(jìn)入到路由組件之前:${to.meta.title}`);
            next()
        },
        beforeRouteLeave(to, from, next){
            console.log(`離開路由組件之前:${from.meta.title}`);
            next()
        }
    }
</script>
// HuBei.vue
<template>
    <div>
        <h2></h2>
        <div>
            <ul>
                <li>
                    <router-link :to="{
                        name : 'wh',
                        params : {
                            a1 : wh[0],
                            a2 : wh[1],
                            a3 : wh[2],
                        }
                    }">
                        武漢市
                    </router-link>
                </li>
                <li>
                    <router-link :to="{
                        name : 'hs',
                        params : {
                            a1 : hs[0],
                            a2 : hs[1],
                            a3 : hs[2],
                        }
                    }">
                        黃石市
                    </router-link>
                </li>
            </ul>
            <router-view></router-view>
        </div>
    </div>
</template>

<script>
    export default {
        name : 'HuBei',
        data(){
            return{
                wh : ['江岸區(qū)', '江漢區(qū)', '橋口區(qū)'],
                hs : ['下陸區(qū)', '鐵山區(qū)', '西塞山區(qū)']
            }
        }
    }
</script>
// App.vue
<template>
    <div>
        <h2></h2>
        <div>
            <ul>
                <li><router-link to="/hubei">湖北省</router-link></li>
            </ul>
            <router-view></router-view>
        </div>
    </div>
</template>

<script>
    export default {
        name : 'App'
    }
</script>
// index.js
import VueRouter from 'vue-router'

import HuBei from '../pages/HuBei'
import City from '../pages/City'

const router = new VueRouter({
    routes : [
        {
            name : 'bei',
            path : '/hubei',
            component : HuBei,
            meta : {title : '湖北省'},
            children : [
                {
                    name : 'wh',
                    path : 'wuhan',
                    component : City,
                    props : true,
                    meta : {
                        isAuth : true,
                        title : '武漢市'
                    },
                },
                {
                    name : 'hs',
                    path : 'huangshi',
                    component : City,
                    props : true,
                    meta : {
                        isAuth : true,
                        title : '黃石市'
                    }
                }
            ]
        }
    ]

})

export default router

原文鏈接:https://blog.csdn.net/weixin_47957908/article/details/134346232

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新