我有2个文件jsx:不能/使用反应路由器
当我访问localhost:8080这是正常的。但是当我访问本地主机:8080/home它是浏览器中的显示消息无法获取/首页
我该如何解决?
Home.jsx:
import React from 'react';
class Home extends React.Component{
constructor(props) {
super(props);
}
render(){
return(
<div>
<h1>Home ...</h1>
</div>
)
}
}
export default Home;
index.jsx:
import React from 'react';
import {render} from 'react-dom';
import Home from './Home.jsx';
import { Router, Route, Link, browserHistory, IndexRoute } from 'react-router';
class App extends React.Component {
render(){
return(
<div>
<ul>
<li>Home</li>
</ul>
{this.props.children}
</div>
);
}
}
render((
<Router history = {browserHistory}>
<Route path = "/" component = {App}>
<IndexRoute component = {Home} />
<Route path = "home" component = {Home} />
</Route>
</Router>
), document.getElementById('app'));
文件webpack.config.js:
var webpack = require('webpack');
var path = require('path');
var BUILD_DIR = path.resolve(__dirname, 'src/client/public');
var APP_DIR = path.resolve(__dirname, 'src/client/app');
var config = {
entry: APP_DIR + '/index.jsx',
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
devServer: {
inline: true,
port: 8080,
contentBase: "./src/client",
hot: true
},
module : {
loaders : [
{
test : /\.jsx?/,
include : APP_DIR,
loader : 'babel-loader',
query: {
presets: ['es2015', 'react']
}
}
]
}
};
module.exports = config;
嗨大卫·特赖恩。我加了,但是当我访问localhost:8080/home它显示在相同的结果localhost:8080。它不切换到主页。你可以帮我吗 ? –
现在你有'IndexRoute'和'home'路线指向同一个地方。所以,你可能会得到相同的结果。尝试重新配置你的'路线'。 –
渲染(( <路由器历史= {browserHistory}> <路由路径= “/” 成分= {应用}> \t <路由路径= “家” 成分= {主页} /> ),document.getElementById('app'));它不工作:( –