通过名称和参数获取 vue-router 的路由 [英] Get route by name and params for vue-router

查看:116
本文介绍了通过名称和参数获取 vue-router 的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Vue 与 vue-router 一起使用.对于列表视图中的产品项目,我想生成 JSON-LN 注释,并将 url 属性设置为产品详细信息视图的路径.

I am using Vue with vue-router. For product items in a list view I would like to generate JSON-LN annotations with the url attribute set to the path of the product detail view.

我知道我可以通过使用 this.$route.path 获得当前路线的路径,但是有没有办法获得一个独特的路线路径,因为它会用

I know I can get the current route's path by using this.$route.path but is there a way to get a distinct route path as it would be rendered with

<router-link :to={name: 'ProductDetail', params: {id: some_id, slug: some_slug}}></router-link>

在其他地方注入路由的路径?

to inject the route's path somewhere else?

推荐答案

您正在寻找 路由器实例的resolve方法:

You are looking for the Router instance's resolve method:

以与 <router-link/> 中使用的相同形式的给定位置,返回具有以下解析属性的对象:

Given location in form same as used in <router-link/>, returns object with the following resolved properties:

{
  location: Location;
  route: Route;
  href: string;
}

在你的情况下,你可以做这样的事情来获取网址:

In your case you could do something like this to get the url:

let props = this.$router.resolve({ 
  name: 'ProductDetail',
  params: { id: some_id, slug: some_slug },
});

return props.href;

这篇关于通过名称和参数获取 vue-router 的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆