router.addRoutes(currentRoute)语句和
router.currentRoute.meta关系?
实验了一下,存的是meta,取的也是同一个meta是一个数据,但是语法上有疑惑,看不懂
```export function initRouterInfo() {
const currentRoutes = router.options.routes
const rightList = store.state.rights
rightList.forEach(item => {
item.children.forEach(item => {
const itemRule = ruleMapping[item.path];
itemRule.meta = item.rights;
currentRoutes[2].children.push(itemRule)
})
})
// console.log("currentRoutes", currentRoutes)
_ **router.addRoutes(currentRoutes)**_
console.log("router添加currentRoutes",router)
}
import router from '@/router.js'
Vue.directive('permission', {
inserted: function(el, binding){
// console.log("el",el);
const action = binding.value.action
//currentRoute哪来的不知道,但是很好用,获取当前路由信息
//
console.log("route当前路由",router)
_**const rightList = router.currentRoute.meta**_
// console.log("meta不能显示吧",router.currentRoute)
const effect = binding.value.effect
if(rightList.indexOf(action) == -1){
if(effect==="disabled"){
el.disabled = true
el.classList.add('is-disabled')
}else{
el.parentNode.removeChild(el)
}
}
}
})
```
看不懂呢,一般是vue的路由的某些API没怎么见过,你可以vue的官网查找自己不熟悉的API
router.addRoutes 就是动态添加路由规则,
router.currentRoute 就是当前的路由对象信息, meta是路由上面绑定的属性,
https://v3.router.vuejs.org/zh/api/#router-currentroute 附上vue-router@3 版本的API网址
看函数功能和自行调试就懂了
addRoutes
是动态的向路由表里添加路由的,addRoutes
已经废弃了取而代之的是addRoute
currentRoute
是获取当前路由的路由信息对象的
router.addRoutes(routes: Array<RouteConfig>)
动态添加更多的路由规则。参数必须是一个符合 routes 选项要求的数组。