这是我的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 时,它无法正常工作。这是代码:
运行良好,应用程序在 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。我收到一个错误:无法获取/about。这不是我期望的,为什么会无法渲染呢?
<div></div>
?你应该在不同的路由之间放置<Switch></Switch>
。https://reacttraining.com/react-router/web/api/Switch - Björn Böing