$router 可以設(shè)置路由
$route 只可以讀取路由信息
如果要在路由中自定義路由信息的標(biāo)識都许,可以在每個路由中通過設(shè)置meta(元數(shù)據(jù))屬性來配置油坝,如: meta: {
? ? num: 0
}
路由攔截/路由導(dǎo)航守衛(wèi)
// 路由攔截/路由導(dǎo)航守衛(wèi)(在路由這個文件里面寫),路由導(dǎo)航守衛(wèi):前置導(dǎo)航守衛(wèi)和后置導(dǎo)航守衛(wèi)
// 三個參數(shù)
// to代表即將進(jìn)入的路由
// from代表即將離開的路由
// next()解幽,每一個導(dǎo)航守衛(wèi)必須至少搭配一個next(),符合條件后只有調(diào)用next()才會進(jìn)行跳轉(zhuǎn),否則會一直卡在沒跳轉(zhuǎn)那個地方
router.beforeEach((to, from, next) => {
? // 想要進(jìn)入購物車頁面燎孟,必須有token
? // console.log('to:', to)
? // console.log('from:', from)
? const token = localStorage.getItem('token')
? if (to.path === '/cart') {
? ? // 此時必須要有token
? ? if (token) {
? ? ? // 這個next()是只針對cart路由的
? ? ? next()
? ? } else {
? ? ? vm.$toast('請先登錄!')
? ? ? // 定時器
? ? ? setTimeout(() => {
? ? ? ? next('/user')
? ? ? }, 1000)
? ? }
? ? return false
? }
? // 下方這個next()是適配所有路由的
? next()
})