【发布时间】:2019-07-07 19:56:44
【问题描述】:
我正在尝试使用根据 URL 路由不同组件的开关。这个 URl 应该可以嵌套,比如courses/:cid/assignments/:aid。
这就是我的 Switch 的样子:
<Switch>
<Route path='/courses/:cid/assignments/:aid' component={Assignment} />
<Route exact path="/" component={Home} />
</Switch>
应用程序运行良好:我可以访问主页。但是,当我访问例如 courses/1/assignments/20 时,我收到以下错误:
Loading failed for the <script> with source “http://localhost:8081/courses/1/assignmentsets/bundle.js”.
我摆弄了一下路径,只是想看看哪里出了问题,结果如下:
<Switch>
<Route path='/courses/:cid/assignments/:aid' component={Assignments} /> // Does not work
<Route path="/courses/randomnestedurl" component={Assignments} /> // Does not work
<Route path="/courses" component={Assignments} /> // Works fine
<Route path="/:aid" component={Assignments} /> // Works fine
<Route exact path="/" component={Home} /> // Works fine
</Switch>
有人知道可能出了什么问题吗?我的 webpack 有什么问题吗?我缺少一些设置?
如果需要提供更多代码,请告诉我我的应用程序需要哪些部分。
编辑:这是我的 webpack.config.js:
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
const APP_PATH = path.resolve(__dirname, 'src');
module.exports = {
entry: APP_PATH,
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, '/dist'),
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.json']
},
module: {
rules: [
{ test: /\.(ts|js)x?$/, loader: 'babel-loader', exclude: /node_modules/ },
{ test:/\.(s*)css$/, use:['style-loader','css-loader', 'sass-loader'] },
],
},
devServer: {
historyApiFallback: true,
proxy: {
'/api': {
target: 'http://localhost:8080',
secure: false
}
}
},
plugins: [
new HtmlWebpackPlugin({ inject: true, template: path.join(APP_PATH, 'index.html') }),
new ForkTsCheckerWebpackPlugin(),
]`enter code here`
};
【问题讨论】:
-
我的 index.html 中没有脚本标签。当我添加一个时,它说只能有一个 babel-polyfil。如果有帮助,我在我的 OP 中添加了 webpack.config。
标签: reactjs webpack react-router