Javascript 网页包未自动编译对React文件中代码的更改
我最近开始尝试使用React和Webpack,在某些内容发生更改时自动编译更改。然而,尽管我在谷歌上搜索到了心满意足的内容,但我似乎无法找到它为什么不起作用 app.jsJavascript 网页包未自动编译对React文件中代码的更改,javascript,reactjs,webpack,babeljs,Javascript,Reactjs,Webpack,Babeljs,我最近开始尝试使用React和Webpack,在某些内容发生更改时自动编译更改。然而,尽管我在谷歌上搜索到了心满意足的内容,但我似乎无法找到它为什么不起作用 app.js import React from 'react' import ReactDOM from 'react-dom' import {Router, Route, Link} from 'react-router' import ProductList from './products' class Main extends
import React from 'react'
import ReactDOM from 'react-dom'
import {Router, Route, Link} from 'react-router'
import ProductList from './products'
class Main extends React.Component {
constructor(props){
let products = [];
super(props);
this.state = {
name: "Harry"
};
}
render(){
return(
<Storefront name={this.state.name} />
);
}
}
var a = 10;
let Storefront = (props) =>
<div className="container">
<h4>Welcome to the React Store Front, {props.name}!</h4>
<Link to="/products">View Products</Link>
</div>;
ReactDOM.render(
<Router>
<Route path="/" component={Main}/>
<Route path="/products" component={ProductList}/>
</Router>,
document.getElementById("app")
);
module.exports = {
context: __dirname,
entry: ['webpack/hot/dev-server', './app.js'],
output: {
filename: "app.js",
path: __dirname + "/dist"
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loaders: ["babel-loader?presets[]=react,presets[]=es2015"]
},
{
test: /\.jsx$/,
loaders: ['babel-loader?presets[]=react,presets[]=es2015']
}
]
},
resolve: {
extensions: ['', '.js', '.jsx']
},
watch: true
};
webpack.config.js
import React from 'react'
import ReactDOM from 'react-dom'
import {Router, Route, Link} from 'react-router'
import ProductList from './products'
class Main extends React.Component {
constructor(props){
let products = [];
super(props);
this.state = {
name: "Harry"
};
}
render(){
return(
<Storefront name={this.state.name} />
);
}
}
var a = 10;
let Storefront = (props) =>
<div className="container">
<h4>Welcome to the React Store Front, {props.name}!</h4>
<Link to="/products">View Products</Link>
</div>;
ReactDOM.render(
<Router>
<Route path="/" component={Main}/>
<Route path="/products" component={ProductList}/>
</Router>,
document.getElementById("app")
);
module.exports = {
context: __dirname,
entry: ['webpack/hot/dev-server', './app.js'],
output: {
filename: "app.js",
path: __dirname + "/dist"
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loaders: ["babel-loader?presets[]=react,presets[]=es2015"]
},
{
test: /\.jsx$/,
loaders: ['babel-loader?presets[]=react,presets[]=es2015']
}
]
},
resolve: {
extensions: ['', '.js', '.jsx']
},
watch: true
};
您必须使用
webpack热中间件
NPM链接
若你们想举例说明,那个么你们可以使用react路由器举例说明
我已使用和我的webpack.config文件
/* eslint-disable no-var */
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: [
'webpack-hot-middleware/client',
'./scripts/index'
],
devtool: 'eval-source-map',
output: {
path: __dirname,
filename: 'bundle.js',
publicPath: '/static/'
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin()
],
module: {
loaders: [{
test: /\.js$/,
loaders: ['babel'],
include: path.join(__dirname, 'scripts')
}]
}
};
您必须使用
webpack热中间件
NPM链接
若你们想举例说明,那个么你们可以使用react路由器举例说明
我已使用和我的webpack.config文件
/* eslint-disable no-var */
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: [
'webpack-hot-middleware/client',
'./scripts/index'
],
devtool: 'eval-source-map',
output: {
path: __dirname,
filename: 'bundle.js',
publicPath: '/static/'
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin()
],
module: {
loaders: [{
test: /\.js$/,
loaders: ['babel'],
include: path.join(__dirname, 'scripts')
}]
}
};
谢谢你的回答。我试着按照npm页面上的步骤进行操作,但当它说“将其添加到服务器”时,我不知道该怎么做。我正在使用LocalHost,谢谢你的回答。我试着按照npm页面上的步骤进行操作,但当它说“将其添加到服务器”时,我不知道该怎么做。我正在使用localhost