【发布时间】:2018-01-14 09:42:37
【问题描述】:
我是 React 新手,我使用这个解释来构建一个基本的 React 应用程序: https://facebook.github.io/react/docs/installation.html
我通过以下方式打开了一个新项目:
npm install -g create-react-app
create-react-app my-app
cd my-app
npm start
index.html 文件位于 hello-world/public 中,其内容如下:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<title>React App</title>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root">
</div>
</body>
</html>
然而,所有“React 的东西”都放在 hello-world\src 目录中。看起来 'hello-world\src\index.js' 激活了组件。
我不明白的是 index.html 如何“读取” index.js 的内容。 index.html 没有指向 index.js 的 html 标记。
我可以通过“npm start”来启动这个应用程序。该命令的作用是提升在 localhost:3000 上运行的服务器。不使用“npm start”如何查看应用程序?
【问题讨论】:
-
这是因为 react 将自己置于
<div id="root"> </div>之间。 -
我明白了。我想要的只是双击 index.html 并查看整个应用程序(无需使用“npm start”设置服务器)
-
您的意思是在
dist/文件夹中? -
@CrazySynthax 只需单击 index.html 即可查看您的内容,您应该使用 webpack 之类的工具,它将所有反应组件代码构建在一个文件中,该文件可能命名为 bundle.min.js然后你只需要将 bundle.min.js 包含在你的 index.html 中