获取react-router中某条Route的路径 [英] Get the path of a certain Route in react-router

查看:36
本文介绍了获取react-router中某条Route的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 react 中使用 react-router 我已经声明了一些这样的路由:

Using react-router in with react i have declared some routes like this:

<Route name="voteview" path="voteview/:baselineId" handler={VoteView}/>

在 React 组件中,我想调用此路由.React Router 给了我一个链接:

In a React Component, i want to call this route. React Router gives me a link:

<Link to="voteview" params={{baselineId: this.props.data.baselineId}}>Voteview</Link>

这会产生一个 链接.

This results in a <a href...> link.

但我想以编程方式使用 url 路径,而不是在 <a href...> 链接中.如何以编程方式获取voteview"路由的 URL 路径?

But i want to use the url path programmatically, not in an <a href...> link. How can i get the URL path to the "voteview" route programmatically?

推荐答案

你可以在 React Routers 上下文中使用 makeHref 方法来生成这样的路径:

You can use the makeHref method on React Routers context to generate the path like this:

this.context.router.makeHref('voteview', {baselineId: this.props.data.baselineId})

这篇关于获取react-router中某条Route的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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