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

學無先后,達者為師

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

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

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

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

path守衛(wèi)(beforeEnter)

  • 代碼位置:path守衛(wèi)代碼寫在route對象中(index.js文件)
  • beforeEnter是一個函數(shù),參數(shù)上沒有回調函數(shù)
  • beforeEnter有三個參數(shù):
    • to參數(shù):to是一個路由對象,表示到哪兒去(跳轉的下一個路由組件)。
    • from參數(shù):form是一個路由對象,表示從哪來(從哪個路由切換過來的)。
    • next參數(shù):next是一個函數(shù),表示允許通過,可以繼續(xù)向下走。
  • 格式:beforeEnter(to, from, next){}
  • beforeEnter執(zhí)行時機:在進入到path守衛(wèi)所在的路由前會被調用

在這里插入圖片描述

// 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 : '武漢市'
                    },
                    // path守衛(wèi)
                    beforeEnter(to, from, next){
                        // 這里設置了登錄名為root
                        let loginName = 'root'
                        if(loginName === 'admin'){
                            next()
                        }else{
                            alert('對不起,您沒有權限')
                        }
                    }
                },
                {
                    name : 'hs',
                    path : 'huangshi',
                    component : City,
                    props : true,
                    meta : {
                        isAuth : true,
                        title : '黃石市'
                    }
                }
            ]
        }
    ]

})

export default router
// 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>
// 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>
// 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']
    }
</script>

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

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