与 React-Router 的相对链接 [英] Relative Linking with React-Router

查看:67
本文介绍了与 React-Router 的相对链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们怎么做?特别是当路径的一部分是参数 (:someParam) 时.我在文档中找不到任何内容.

How do we do it? Especially when part of the path is a param (:someParam). I can't find anything in the documentation.

推荐答案

现在推荐使用 react-router 3 这个场景https://github.com/ReactTraining/react-router/issues/3325

It's now recommended to use react-router 3 for this scenario https://github.com/ReactTraining/react-router/issues/3325

对于任何在 react-router 2.x 中遇到此问题的人:

For anyone having this problem with react-router 2.x:

您可以使用 browserHistory 来支持相对路由,而不是使用 Link 组件.

You may use browserHistory to support relative routes, instead of using the Link component.

    // assuming http://site/myanimals

    // configure router for hosting in subdirectory
    const history = useRouterHistory(createHistory)({
       basename: '/myanimals'
    });

    // then use it ...
    import {browserHistory} from 'react-router';

    browserHistory.push('about');

    // or, for a configured route with 'animal/:id' you can use 

    // <Route path={'animal:id'} .../>           
    browserHistory.push('animal/3');

我希望这会有所帮助!

这篇关于与 React-Router 的相对链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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