我正在使用create-react-app,我需要在没有服务器的情况下直接在浏览器中打开build/index.html [英] I'm using create-react-app and I need to open build/index.html in browser directly without a server

查看:51
本文介绍了我正在使用create-react-app,我需要在没有服务器的情况下直接在浏览器中打开build/index.html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力寻找解决此问题的方法.我正在使用create-react-app,运行npm build之后,我希望能够从文件位置使用"doubleclick"在浏览器中打开index.html,而无需npm或任何服务器.我还希望我的应用程序的路由和功能在这种情况下能够正常工作.我一直在对此事进行研究,但找不到很多.我想知道是否有可能,是否有人可以共享任何链接甚至解决方案,以防万一我丢失了某些东西.将不胜感激.

I'm struggling to find the solution to this issue. I'm using create-react-app and after running npm build I want to be able to open index.html in browser with 'doubleclick' from file location, without the need of npm or any server. I also want my routes and functionality of the app to work in this scenario. I've been doing some research on the matter but couldn't find much. I would like to know if it's possible and if anyone has any links or even solutions to share in case I'm missing something. Would be greatly appreciated.

更新:

我正在使用ubuntu17.src/App.js中的主要路由如下所示:

I'm using ubuntu 17. My main route in src/App.js looks like so :

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

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

And my index.js looks like so:

import App from './component/App';


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

运行 npm运行build 并在浏览器中打开build/index.html之后,它是蓝色的(背景),所以当我检查它的内部带有注释时,它会出现一些css.我正在尝试使该应用程序在浏览器中运行,而不使用任何类型的服务器.只需从浏览器的build文件夹中打开index.html.

After running npm run build and open build/index.html in browser, it's blue (the background) so some css gets to it, when i inspect it's showing with the comment inside it. I'm trying to get the app working in browser without using any kind of server. Just by opening index.html from build folder in the browser.

注意:一切都可以在dev中完美运行

Note: everything works perfectly in dev

非常感谢.

推荐答案

我发现使用< HashRouter> 而不是< Router> 可以解决此问题.

I found that using <HashRouter> instead of <Router> fixed it.

非常感谢

这篇关于我正在使用create-react-app,我需要在没有服务器的情况下直接在浏览器中打开build/index.html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆