路由獨享-組件內守衛


路由獨享寫法:
import VueRouter from 'vue-router'
Vue.use(VueRoter)
const router = new VueRouter({
    routes:[
        {path:component:,name:,beforeEnter:((to,next,from)=>{
            alert('路由獨享-組件內守衛')
            next()//跳轉
            next(false)//不顯示此路由下的組件
        })}
    ],
    mode:'history',
})
組件內守衛:在組件內寫
export defalut{
    data(){
        return {
            name:'tom'
        }
    },
    beforeRouteEnter((to,from,next)=>{
        //beforeRouteEnter 守衛 不能 訪問 this,因為守衛在導航確認前被調用,因此即將登場的新組件還沒被創建。
        //可以通過next()回調函數獲得組件實例
        next(vm=>{
            vm.name
        })

   })
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM