【发布时间】:2018-02-12 11:36:10
【问题描述】:
我有一个基于 TS 的简单 React 项目并使用 webpack。我的问题是路由器没问题,一切都正确重定向并且工作正常,但是当我刷新页面时,我有无法获取错误。
index.tsx
ReactDOM.render(
<div>
<Router>
<div className="App">
<Route exact path='/' component={Lessons}/>
<Route path='/watch/:name' component={CurrentLesson}/>
</div>
</Router>
</div>,
document.getElementById("example"));
webpack.config.js
module.exports = {
entry: "./src/index.tsx",
output: {
filename: "bundle.js",
path: __dirname + "/dist",
publicPath: '/'
},
plugins: [
new webpack.HotModuleReplacementPlugin()
],
devtool: "source-map",
resolve: {
extensions: [".ts", ".tsx", ".js", ".json"]
},
module: {
loaders: [
{
test: /\.tsx?$/,
loader: "awesome-typescript-loader"
}
]
},
externals: {
"react": "React",
"react-dom": "ReactDOM"
},
devServer: {
historyApiFallback: true,
hot: true
}};
我读到历史可能存在问题,但对于 webpack,建议是放置 output.PublicPath 和 devServer.historyApiFallback 并让反应路由器传递历史对象,但由于我使用反应路由器 dom,它已经有一个历史对象还有一点是,当我在纯 ES6 上编写完全相同的项目时,没有那种糟糕的废话,一切正常。我已经安装了@types/react-router-dom 4.0.x 版本和 react-router-dom 4.x.x 版本
为什么刷新时我得到无法获取 /watch/some-param
【问题讨论】:
标签: javascript reactjs webpack react-router