admin管理员组文章数量:1394968
In VueJs i can always access the current route data. but what if i want to access a different route's data by its name ? suppose i have routes defined,
[
{
name: "dashboard",
path: "/",
ponent: Dashboard,
meta: {
title: "Dashboard",
}
},
{
name: "login",
path: "/login",
ponent: Login,
meta: {
title: "Login",
}
}
]
now suppose i am in dashboard route. i can access its data by this.$route
but what if i want to access the meta data of route named as login ?
is there any function which will return me the object of route named login
?
In VueJs i can always access the current route data. but what if i want to access a different route's data by its name ? suppose i have routes defined,
[
{
name: "dashboard",
path: "/",
ponent: Dashboard,
meta: {
title: "Dashboard",
}
},
{
name: "login",
path: "/login",
ponent: Login,
meta: {
title: "Login",
}
}
]
now suppose i am in dashboard route. i can access its data by this.$route
but what if i want to access the meta data of route named as login ?
is there any function which will return me the object of route named login
?
2 Answers
Reset to default 6You can use the resolve method to translate route name into a path like this:
const { href } = this.$router.resolve({
name: 'password-change',
});
or if you want more information:
const { route } = this.$router.resolve({
name: 'password-change',
});
in return you get an object containing route path, meta, params etc.
You could access the route using this.$router
this.$router.options.routes[0].meta
Of course you have to grab the correct route
UPDATE
Of course you could iterate over your array:
getRoute: function (routeName) {
let result = this.$router.options.routes.find(i => i.name === routeName)
if (result == null) {
this.$router.options.routes.forEach(element => {
if (element.children) {
let route = element.children.find(i => i.name === routeName)
if (route != null) {
result = route
}
}
})
}
return result
},
本文标签: javascriptHow to get a specific route data by route name in VueJsStack Overflow
版权声明:本文标题:javascript - How to get a specific route data by route name in VueJs? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744112689a2591361.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论