首页
注册
登录
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请
登录
广告
V2EX
›
问与答
家人们,想问一个 react 路由相关的问题,求大佬解答。
Croow
·
17 天前
· 522 次点击
场景:
我接触 react 一个月了,想试一下 cicd 发布项目,我写的一个纯前端的项目,打包后上传到腾讯云,用指令:cross-env PUBLIC_URL=xxx 腾讯云存储桶地址 node scripts/build.js ,把所有资源导入方式都改成了腾讯云的地址。
问题:
但是我访问 index.html 时一直是白屏,查了半天发现 js ,css 等资源获取成功了,原因在 index.html 读取不到路由。
解决办法:
然后在 index.js 把
import {BrowserRouter} from 'react-router-dom';
root.render(
<BrowserRouter>
<App />
</BrowserRouter>
);
改为 hash 模式,发现就能正常访问了。
import {HashRouter as Router} from 'react-router-dom';
root.render(
<Router >
<App />
</Router>
);
疑问:
为啥要 hash 才行呢?那生产环境发布代码大多数也不是用的 hash 吧
React
路由
Hash
3 条回复
1
iOCZS
17 天前
因为 history 模式对应的路由,服务端没有
2
laobobo
17 天前
懒得贴了,自己去查查 hash 模式 和 history 模式的 区别就知道了,
3
IvanLi127
17 天前
有一种方案是在每个子目录都放 index.html ,应该能配出来
关于
·
帮助文档
·
博客
·
API
·
FAQ
·
实用小工具
·
6095 人在线
最高记录 6679
·
Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 29ms ·
UTC 02:01
·
PVG 10:01
·
LAX 18:01
·
JFK 21:01
Developed with
CodeLauncher
♥ Do have faith in what you're doing.