【发布时间】:2017-12-01 23:59:34
【问题描述】:
这是我的 webpack 配置代码:
const compiler = webpack({
entry: ['whatwg-fetch', path.resolve(__dirname, 'js', 'app.js')],
module: {
loaders: [
{
exclude: /node_modules/,
test: /\.js$/,
loader: 'babel',
},
],
},
output: {filename: 'app.js', path: '/'},
});
const app = new WebpackDevServer(compiler, {
contentBase: '/public/',
proxy: {'/graphql': `http://localhost:${GRAPHQL_PORT}`},
publicPath: '/js/',
stats: {colors: true},
});
app.use('/', express.static(path.resolve(__dirname, 'public')));
工作正常,应用程序在 localhost:3000/index.html 上运行,但是当我尝试实现 React Router dom v4 时。它不起作用。这是代码:
const About = () => <div> <h1>About</h1> </div>
ReactDOM.render(
<BrowserRouter>
<div>
<Route exact path='/' component={App}/>
<Route path='/about' component={About}/>
</div>
</BrowserRouter>,
mountNode
);
这是 localhost:3000/ 上的结果 在 localhost:3000/about 上。我收到一个错误:无法 GET /about 。不是我所期待的,为什么这不会呈现?
关于
【问题讨论】:
-
你为什么要在你的路由组件周围添加一个额外的
<div></div>?您应该将<Switch></Switch>放在不同的路线周围reacttraining.com/react-router/web/api/Switch -
@Bjoern 我尝试了 Switch,但也不起作用。我使用了那个 div,因为路由器只想要一个孩子。
-
嘿,你找到答案了吗?我也遇到了同样的问题...
标签: reactjs webpack react-router webpack-dev-server react-router-v4