首页 > 解决方案 > 使用 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;

标签: reactjsreact-routeryarnpkg

解决方案


推荐阅读