我可以在 react-router 中设置基本路由吗 [英] Can I set a base route in react-router

查看:44
本文介绍了我可以在 react-router 中设置基本路由吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的应用的基本网址是 example.com/app

Assuming my app's base url is example.com/app

是否可以在 react-router 中设置基本路由,而不是将所有路由都写为

Is it possible to set a base route in react-router so instead of writing all routes as

/app/a
/app/b
/app/c

我可以将它们指定为

a
b
c

我尝试了在 docs 中找到的以下示例 但它不起作用(页面将不显示任何内容).也许是因为我使用的是 react-router@3.0.0-alpha.1,或者我做错了什么.

I tried the below example I found in the docs but it wouldn't work (page would display nothing). Maybe it's because I'm using react-router@3.0.0-alpha.1, or I'm doing something wrong.

import { useRouterHistory } from 'react-router'
import { createHistory } from 'history'

const history = useRouterHistory(createHistory)({
  basename: '/app'
})

const Root = ({store}) => (
    <Provider store={store}>
        <Router history={history}>
            <Route path='/' component={App}>
                ...
            </Route>
        </Router>
    </Provider>
)

推荐答案

使用最新的 react router (v4) 你可以很容易地做到这一点

With the newest react router (v4) you can easily do this

<BrowserRouter basename="/calendar">
  <Link to="/today"/> // renders <a href="/calendar/today">
</BrowserRouter>

这篇关于我可以在 react-router 中设置基本路由吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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