使用反应路由器设置基本名称 [英] Set basename with react router
问题描述
我正在使用 react-router,其中,我正在使用 hashRouter.现在我必须在我网站的子目录中部署我的 React 应用程序.为此,我做到了:
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) 在 package.json
文件中添加主页参数
2) 添加
1) Added homepage parameter in package.json
file
2) Added <HashRouter basename={process.env.PUBLIC_URL}>
现在,当我访问我的网站 URL localhost/react
时,我将被重定向到 http://localhost/#/react/apps/
.
Now, When I access my website URL localhost/react
, I will be redirected to http://localhost/#/react/apps/
.
但我希望它被重定向到 http://localhost/react/#/apps/
.
But I want it to be redirected to http://localhost/react/#/apps/
.
注意:我必须部署 React 生产版本.
Note: I have to deploy react production build.
我的生产构建结构:
--- 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.
推荐答案
如果您查看 basename 的文档,它会在哈希符号后附加字符串:
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
如果您删除 basename,并且您的 index.html 是从/reactapp 提供的 - 它可能会正常工作.
If you remove basename, and your index.html is served from /reactapp - it may just work.
这篇关于使用反应路由器设置基本名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!