meta字段(元數據)
直接在路由配置的時候,給每個路由添加一個自定義的meta對象,在meta對象中可以設置一些狀態,來進行一些操作。
例如:登陸驗證
{
path: '/actile',
name: 'Actile',
component: Actile,
meta: {
login_require: false
},
},
{
path: '/goodslist',
name: 'goodslist',
component: Goodslist,
meta: {
login_require: true
},
children:[
{
path: 'online',
component: GoodslistOnline
}
]
}
//在導航守衛中可以這樣使用
router.beforeEach((to, from, next) => {
if (to.matched.some(function (item) {
return item.meta.login_require
})) {
next('/login')
} else
next()
})
只需要判斷item下面的meta對象中的login_require是不是true,就可以做一些限制了。
原文章轉載:https://blog.csdn.net/cofecode/article/details/79181894