Xia*_*ong 13 javascript reactjs webpack react-router
我用Webpack和react-rounter构建了一个项目.这是我的代码:
ReactDOM.render(
<Provider store={store}>
<Router history={ browserHistory }>
<Route path='/' component={ App } >
<IndexRoute component={ Home } />
<Route path="purchase" component={ Purchase } />
<Route path="purchase/:id" component={ Purchase } />
</Route>
</Router>
</Provider>,
document.getElementById('example')
);Run Code Online (Sandbox Code Playgroud)
当我要求时"http://127.0.0.1:3001/purchase",它的工作!但地址"http://127.0.0.1:3001/purchase/a"有错误.查看错误消息:在此处输入图像描述
我的WebpackDevServer配置是:
new WebpackDevServer (webpack(config), {
publicPath: config.output.publicPath,
hot: true,
noInfo: false,
historyApiFallback: true
}).listen(3001, '127.0.0.1', function (err, result) {
if (err) {
console.log(err);
}
console.log('Listening at localhost:3001');
});Run Code Online (Sandbox Code Playgroud)
我不知道是怎么回事,救救我!
Sey*_*han 28
您正在使用相对路径来描述index.html中的bundle.js的路径.
您应该在index.html中使用bundle.js的绝对路径
绝对路径包含根目录和包含文件或文件夹的所有其他子目录.
如果它与index.html在同一路径中.
例如.
public/index.html
public/bundle.js
这可以解决您的问题.
<script src="/bundle.js"></script>
Run Code Online (Sandbox Code Playgroud)
Jua*_*rco 12
如果你碰巧使用了HtmlWebpackPlugin直接编辑index.html是不是一种选择。因此,要解决此特定问题,请添加publicPath并指定/为其中的根文件夹webpack.config.js:
const path = require('path')
module.exports = {
entry: './app/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: '/'
},
// more config stuff goes below..
}
Run Code Online (Sandbox Code Playgroud)
不要忘记重新启动 webpack 开发服务器以使这些更改生效
关于publicPath 这里的更多信息。
| 归档时间: |
|
| 查看次数: |
5316 次 |
| 最近记录: |