我的页面没有根据指定的路由进行路由。我的文件就是这样。目前我的Home.js和Main.js运行良好。但是当我尝试路由到playerOne时,什么也没有发生。IE。页面保持不变,没有任何变化。
我对反应完全陌生,并且正在努力学习。我不知道我错在哪里了。有人可以帮帮我吗??提前致谢。
路由.js
var React = require('react');
var ReactRouter = require('react-router');
var Router = ReactRouter.Router;
var Route = ReactRouter.Route;
var hashHistory = ReactRouter.hashHistory;
var IndexRoute = ReactRouter.IndexRoute;
var Main = require('../components/Main');
var Home = require("../components/Home");
var PromptContainer = require('../containers/PromptContainer');
var routes = (
<Router history={hashHistory}>
<Route path='/' component={Main}>
<IndexRoute component={Home} />
<Route path='playerOne' component={PromptContainer} />
<Route path='playerTwo/:playerOne' component={PromptContainer} />
</Route>
</Router>
);
module.exports = routes;
PrompContainer.js
var React= require('react');
var PromptContainer= React.createClass({
render: function(){
console.log(this);
return(
<div className="jumbotron col-sm-6 col-sm-offset-3 text-center">
<h1>Some Text</h1>
<div className="col-sm-12">
<form>
<div className="form-group">
<input
type="text"
placeholder="Username"
className="form-control"
/>
</div>
<div className="form-group col-sm-4 col-sm-offset-4">
<button
className="btn btn-block btn-success"
type="submit">
Continue
</button>
</div>
</form>
</div>
</div>
)
}
});
module.exports= PromptContainer;
主.js
var React=require('react');
var Main=React.createClass({
render:function(){
return(
<div className="main-container">
{this.props.children}
</div>
)
}
});
module.exports= Main;
主页.js
var React=require('react');
var Home=React.createClass({
render:function(){
return(
<div>
Hello From Home!!!
</div>
)
}
});
module.exports= Home;
index.js
var React=require('react');
var ReactDOM=require('react-dom');
var routes=require('./config/routes');
ReactDOM.render(
routes,
document.getElementById("app")
);
我的入口点是index.js文件。我的目录结构是这样的——
>app
>components
Home.js
Main.js
>config
routes.js
>containers
PromptContainer.js
index.js
index.html
>dist
>node_modules
package.json
webpack.config.js
我的 package.json 文件是 -
{
"name": "react",
"version": "1.0.0",
"description": "Learning React",
"main": "index.js",
"scripts": {
"test": "ava 'app/**/*.test.js' --verbose --require ./other/setup-ava-tests.js",
"production": "webpack -p",
"start": "webpack-dev-server"
},
"dependencies": {
"jquery": "^3.0.0",
"npm": "^3.9.6",
"react": "^15.1.0",
"react-dom": "^15.1.0",
"react-router": "^2.4.1",
"webpack": "^1.13.1"
},
"devDependencies": {
"babel-core": "^6.9.1",
"babel-loader": "^6.2.4",
"babel-preset-react": "^6.5.0",
"coffee-loader": "^0.7.2",
"html-webpack-plugin": "^2.21.0",
"webpack": "^1.13.1",
"webpack-dev-server": "^1.14.1"
},
"author": "",
"license": "ISC"
}
我的 Webpack 配置是
var HtmlWebpackPlugin = require('html-webpack-plugin')
var HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
template: __dirname + "/app/index.html",
filename: "index.html",
inject: "body"
});
module.exports = {
entry: [
'./app/index.js'
],
module: {
loaders: [{
test: /\.js$/,
exclude: /node_modules/,
loader: "babel-loader"
}]
},
output: {
filename: "bundle.js",
path: __dirname + "/dist"
},
plugins: [HtmlWebpackPluginConfig]
};