我可以在react-router中设置基本路由

前端之家收集整理的这篇文章主要介绍了我可以在react-router中设置基本路由前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
假设我的应用程序的基本URL是example.com/app

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

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

我可以将它们指定为

a
b
c

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

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),您可以轻松完成此操作
<BrowserRouter basename="/calendar"/>
<Link to="/today"/> // renders <a href="/calendar/today">
原文链接:https://www.f2er.com/react/301353.html

猜你在找的React相关文章