我正在使用 create-react-app,我需要在没有服务器的情况下直接在浏览器中打开 build/index.html

IT技术 javascript node.js reactjs webpack create-react-app
2021-05-06 20:09:21

我正在努力寻找这个问题的解决方案。我正在使用 create-react-app 并且在运行 npm build 后,我希望能够在浏览器中使用文件位置的“双击”打开 index.html,而无需 npm 或任何服务器。我还希望我的应用程序的路线和功能能够在这种情况下工作。我一直在对此事进行一些研究,但找不到太多。我想知道是否有可能,是否有人有任何链接甚至解决方案可以分享,以防万一我遗漏了什么。将不胜感激。

更新:

我正在使用 ubuntu 17。我在 src/App.js 中的主要路线如下所示:

class App extends React.Component {
  render(){
    return(
      <Router basename="/">
        <Switch>
            <Route exact path="/" component={LoginComponent}/>
            <Route path="/app" component={AppLayout}/>
        </Switch>
      </Router>
    );
  }
}

我的 index.js 看起来像这样:

import App from './component/App';


ReactDOM.render(<App />, document.getElementById('root'));

npm run build在浏览器中运行并打开 build/index.html 后,它是蓝色的(背景)所以一些 css 到达它,当我检查它时,它显示了其中的注释。我正在尝试让应用程序在浏览器中运行而不使用任何类型的服务器。只需从浏览器的构建文件夹中打开 index.html 即可。

注意:在开发中一切正常

提前谢谢了。

1个回答

我发现使用<HashRouter>而不是<Router>修复它。

非常感谢