Set basename with react router

随声附和 提交于 2021-02-08 11:12:52

问题


I am using react-router and in that, I am using hashRouter. Now i have to deploy my react app in sub-directory of my website. So for that, I did:

1) Added homepage parameter in package.json file
2) Added <HashRouter basename={process.env.PUBLIC_URL}>

Now, When I access my website URL localhost/react, I will be redirected to http://localhost/#/react/apps/.

But I want it to be redirected to http://localhost/react/#/apps/.

Note: I have to deploy react production build.

My production build structure:

--- static
--- asset-manifest.json
--- index.html
--- manifest.json
--- service-worker.js

Please help me, How can I achieve that? Thanks in advance for your time and answer.


回答1:


If you look at the documentation for basename, it appends the string after the hash symbol:

https://reacttraining.com/react-router/web/api/HashRouter/basename-string

If you remove basename, and your index.html is served from /reactapp - it may just work.



来源:https://stackoverflow.com/questions/55208238/set-basename-with-react-router

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