How to set a base url for react-router at the app level?

时光毁灭记忆、已成空白 提交于 2019-12-07 04:53:54

问题


I have an app created with create-react-app that I want to install in a subdirectory on my website. The recommended way is to add process.env.PUBLIC_URL as the baseurl. ie:

<Route exact path={`${process.env.PUBLIC_URL}/signin`} component={SigninPage}/>

Now is there a way I can set it at the app level so I don't have to duplicate this piece of code on every Routes or Links or NavLinks?

Thanks!


回答1:


You can set it as basename on your Router, here are links to the react router docs: basename in BrowserRouter and basename in HashRouter. It would look something like this:

<Router basename={process.env.PUBLIC_URL}>
</Router>


来源:https://stackoverflow.com/questions/48134785/how-to-set-a-base-url-for-react-router-at-the-app-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!