Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在VueJs中按路由名称获取特定路由数据?_Javascript_Vue.js_Vuejs2_Vuex_Vue Router - Fatal编程技术网

Javascript 如何在VueJs中按路由名称获取特定路由数据?

Javascript 如何在VueJs中按路由名称获取特定路由数据?,javascript,vue.js,vuejs2,vuex,vue-router,Javascript,Vue.js,Vuejs2,Vuex,Vue Router,在VueJs中,我始终可以访问当前路由数据。但如果我想通过名称访问不同路由的数据,该怎么办? 假设我定义了路线 [ { name: "dashboard", path: "/", component: Dashboard, meta: { title: "Dashboard", } }, { name: "login", path: "/

在VueJs中,我始终可以访问当前路由数据。但如果我想通过名称访问不同路由的数据,该怎么办? 假设我定义了路线

[
    {
        name: "dashboard",
        path: "/",
        component: Dashboard,
        meta: {
            title: "Dashboard",
        }
    },
    {
        name: "login",
        path: "/login",
        component: Login,
        meta: {
            title: "Login",
        }
    }
]
现在假设我在仪表板路线上。我可以通过
this.$route
访问它的数据,但是如果我想访问名为login的route的元数据呢?
是否有任何函数将返回名为
login
的路由对象?

您可以使用此。$router访问路由

this.$router.options.routes[0].meta
当然,你必须抓住正确的路线


更新

当然,您可以迭代数组:

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
},
您可以使用该方法将路由名称转换为如下路径:

const { href } = this.$router.resolve({
  name: 'password-change',
});
或者,如果您需要更多信息:

const { route } = this.$router.resolve({
  name: 'password-change',
});

作为回报,您将得到一个包含路由路径、元、参数等的对象。

我没有路由的索引。我有路线的名称。也可能有儿童路线。