我是 React 的新手,我关注了Facebook 的安装(创建新应用程序)。
所以每次我需要运行应用程序时,它都需要启动一个服务器。当我尝试在 chrome 中打开构建版本(直接打开 HTML)时,没有显示任何内容。
然后我尝试使用codecademy教程从头开始设置 React 环境。在这里,我建好项目后,可以直接在chrome中打开html,显示内容。
我的问题是:
为什么网页没有在第一种方法中显示,而第二种方法在不启动服务器的情况下运行?
编辑:
第二种方法的package.json:
{
"name": "practice",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack",
"start": "webpack-dev-server"
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^15.6.1",
"react-dom": "^15.6.1"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-react": "^6.24.1",
"html-webpack-plugin": "^2.30.1",
"webpack": "^3.6.0",
"webpack-dev-server": "^2.8.2"
}
}
webpack.config.js :
var HTMLWebpackPlugin = require('html-webpack-plugin');
var HTMLWebpackPluginConfig = new HTMLWebpackPlugin({
template: __dirname + '/app/index.html',
filename: 'index.html',
inject: 'body'
});
module.exports = {
entry: __dirname + '/app/index.js',
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
}
]
},
output: {
filename: 'transformed.js',
path: __dirname + '/build'
},
plugins: [HTMLWebpackPluginConfig]
};


