首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React Router 4,如何使用route或withRouter获得有效的匹配url?

React Router 4,如何使用route或withRouter获得有效的匹配url?
EN

Stack Overflow用户
提问于 2017-05-29 09:19:07
回答 2查看 2.8K关注 0票数 4

location对象正确。匹配url属性始终为'/‘

这是一个显示problem React Router 4 match woes的代码笔

代码语言:javascript
复制
const { render } = ReactDOM

    const {
      HashRouter,
      Route,
      Link
    } = ReactRouterDOM

    const App = () => (
      <HashRouter>
        <div>
          <AddressBar/>

          <ul>
            <li><Link to="/">Home</Link></li>
            <li><Link to="/about">About</Link></li>
            <li><Link to="/topics">Topics</Link></li>
          </ul>

          <hr/>

          <Route exact path="/" component={Home}/>
          <Route path="/about" component={About}/>
          <Route path="/topics" component={Topics}/>
        </div>
      </HashRouter>
    )

    const Home = () => (
      <div>
        <h2>Home</h2>
      </div>
    )

    const About = ({ match }) => (
      <div>
        <h2>About</h2>
        <h3>Match: {match.url}</h3>
      </div>
    )

    const Topics = ({ match }) => (
      <div>
        <h2>Topics</h2>
        <ul>
          <li><Link to={`${match.url}/rendering`}>Rendering with React</Link></li>
          <li><Link to={`${match.url}/components`}>Components</Link></li>
          <li><Link to={`${match.url}/props-v-state`}>Props v. State</Link></li>
        </ul>

        <Route path={`${match.url}/:topicId`} component={Topic}/>
        <Route exact path={match.url} render={() => (
          <h3>Please select a topic.</h3>
        )}/>
      </div>
    )

    const Topic = ({ match }) => (
      <div>
        <h3>{match.params.topicId}</h3>
        <h3>Match: {match.url}</h3>
      </div>
    )

    const AddressBar = () => (
      <Route render={({ location: { pathname }, match: {url}, goBack, goForward }) => (
        <div className="address-bar">
          <div>
            <button
              className="ab-button"
              onClick={goBack}
            >◀︎</button>
          </div>
          <div>
            <button
              className="ab-button"
              onClick={goForward}
            >▶</button>
          </div>
          <div className="url">URL: {pathname} Match: {url}</div>
        </div>

      )}/>
    )

    render(<App/>, document.getElementById('root'))

单击About链接(主题也有同样的问题):请注意,使用路由渲染的AddressBar始终显示在顶部

代码语言:javascript
复制
> Match: /

Match: /about

我做错了什么?

代码语言:javascript
复制
let keys = []
let path = '/*/:league/:id';
if (this.path.split('/').length === 3)
  path = '/*/:league';
let re = pathToRegexp(path, keys);
const result = re.exec(this.path)
if (result && result[2])
  this.store.setLeague(result[2]);
EN

回答 2

Stack Overflow用户

发布于 2017-08-15 05:19:55

如果我正确理解了您的问题,您希望能够使用React Router的匹配工具从URL中提取参数。这对我很有效

代码语言:javascript
复制
import { matchPath } from 'react-router'

const match = matchPath('/users/123', {
  path: '/users/:id',
  exact: true,
  strict: false
})

来源:https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/api/matchPath.md

票数 1
EN

Stack Overflow用户

发布于 2018-06-29 21:43:44

我想当你需要使用匹配的时候,你也需要把它和路径名结合起来

示例

代码语言:javascript
复制
var match = this.props.match;
<Link className = 'xxx' to = {{pathname:match.url + '/target'}}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44233514

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档