reactjs - 使用 yarn build 构建反应应用程序后,路由无法按预期工作
问题描述
我构建了一个反应应用程序,创建了路由文件,并且当我使用运行应用程序时它按预期工作yarn start
,但是当我尝试构建项目以便将其部署为静态组件并使用运行它
npx serve -l 3000
时,路由不起作用正如预期的那样,404
当我点击索引页面以外的任何页面时,它一直在给我。
The project was built assuming it is hosted at /.
You can control this with the homepage field in your package.json.
The build folder is ready to be deployed.
You may serve it with a static server:
yarn global add serve
serve -s build
const Routes = () => {
return <Router key="router">
<Switch key="swith-router">
<Route exact path="/" key="a">
...
</Route>
<Route path="/b" key="b">
...
</Route>
<Route path="/c" key="c">
...
</Route>
<Route path="/d" key="d">
...
</Route>
<Route path="/e" key="e">
...
</Route>
</Switch>
</Router>
};
export default Routes;
解决方案
推荐阅读
- google-sheets - 如何更正 Google 表格中的解析错误?
- sql - 改进访问查询
- java - 增量实例变量不可用于下一个线程
- ruby - Rails Monkey Patch alias_method 导致无方法错误
- wpf - 为什么这个简单的 WPF ListView 示例会生成延伸到边框之外的文本?
- node.js - 如何使用 mongoose 将多个 refs 保存到 MongoDB 中的一个文档中?
- angular - 在 iPhone X 上使用 Ionic 4
- php - 如何使用智能引号显示旧文本。等与HTML
- ruby-on-rails - 如何在 Ruby on Rails 应用程序中实现自定义 css?
- loops - 批处理 - 使用 for 循环替换文本文件中的行,包括空行