admin管理员组文章数量:1278978
I'm fairly new to vue.js
and I'm currently trying to setup my different routes. I'm using sub routes, since the "logged in" user will have a different UI than a visitor.
Currently my setup is like this:
routes: [
{
path: '/auth',
name: 'auth',
ponent: test,
meta: {
auth: false
},
children: [
{
path: 'login',
name: 'login',
ponent: login
},
{
path: 'signup',
name: 'signup',
ponent: signup
}
]
},
{
path: '/user',
name: 'user',
ponent: test,
meta: {
auth: true
},
children: [
{
path: 'profile',
name: 'profile',
ponent: login
}
]
}
]
While this is working, I'm wondering why child routes don't take over the parents meta
properties. Do I need to assign the meta.auth
to each sub route? Or is there any way to inherit this?
Essentially in the router.beforeEach
, I want to check if the user is authenticated correctly or not. But only on child routes of /user
I'm also ing from an angular
background, so I'm used to nesting routes, not sure if this is the best way in Vue.
I'm fairly new to vue.js
and I'm currently trying to setup my different routes. I'm using sub routes, since the "logged in" user will have a different UI than a visitor.
Currently my setup is like this:
routes: [
{
path: '/auth',
name: 'auth',
ponent: test,
meta: {
auth: false
},
children: [
{
path: 'login',
name: 'login',
ponent: login
},
{
path: 'signup',
name: 'signup',
ponent: signup
}
]
},
{
path: '/user',
name: 'user',
ponent: test,
meta: {
auth: true
},
children: [
{
path: 'profile',
name: 'profile',
ponent: login
}
]
}
]
While this is working, I'm wondering why child routes don't take over the parents meta
properties. Do I need to assign the meta.auth
to each sub route? Or is there any way to inherit this?
Essentially in the router.beforeEach
, I want to check if the user is authenticated correctly or not. But only on child routes of /user
I'm also ing from an angular
background, so I'm used to nesting routes, not sure if this is the best way in Vue.
-
Nested routes sure is the right way to divide the page into reasonable parts. For the inheritence, I feel those code above is most probably treated as plain objects, since they're
config
s, and most frameworks have us declare classes/ponents separately when they want to do inheritence and other pre-process for us. – JJPandari Commented Nov 22, 2016 at 7:16 -
@PanJunjie潘俊杰 In
angular
for example, if you declare a child route, it will inherit the properties from the parent. So I was just wondering how this works invue
. It's not a big deal to declare those properties on each child route, but it would've been easier if i only had to declare them on the parent. – woutr_be Commented Nov 22, 2016 at 7:30
2 Answers
Reset to default 6To answer my own question: https://github./vuejs/vue-router/issues/704
I didn't realise this was deprecated in Vue-router 2.0, it is possible to get the matched route and find the meta there.
With vue-router v.3 to match parent's meta (for example: in beforeEach()
func. ) You need to use to.matched.some()
like this:
router.beforeEach((to, from, next) => {
if (to.matched.some(record => record.meta.auth)) {
// ...
next({name:'route-name'})
} else {
next()
}
}
https://router.vuejs/guide/advanced/meta.html
本文标签: javascriptVue router inherit parent propertiesStack Overflow
版权声明:本文标题:javascript - Vue router inherit parent properties - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741232777a2362409.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论