reactjs - 即使 url 更改,路由组件也不会呈现。使用自定义 webpack
问题描述
下面是我的文件结构
和 webpack.config.js 看起来像下面这样
const path = require("path");
const HtmlWebPackPlugin = require("html-webpack-plugin");
const entryPoint = path.join(__dirname, "src", "index.js");
module.exports = {
entry: entryPoint,
output: {
path: path.join(__dirname, "public"),
filename: "bundle.js",
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
},
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"],
},
],
},
plugins: [
new HtmlWebPackPlugin({
template: path.join(__dirname, "public", "index.html"),
filename: "./index.html",
}),
],
devServer: {
contentBase: path.join(__dirname, "public"),
historyApiFallback: true,
// publicPath: "/dist/",
},
devtool: "source-map",
stats: {
errorDetails: true,
},
};
我不知道为什么它仍然无法正常工作,尽管我已经给出了 historyApiFallback
下面是我的 app.js
当我点击/home
路线时,内容没有改变。
谁能解释一下我还要补充什么
解决方案
您需要添加exact
到您的/
路线,否则它将匹配以开头的任何内容,/
并且由于它在 a 中Switch
,因此它将是唯一匹配的。
<Switch>
<Route exact path="/" component={() => <div>i am in home</div>} />
<Route exact path="/home" component={() => <div> i am user</div>} />
</Switch>
如果 url 没有参数化,我exact
也会/home
添加
推荐阅读
- ms-access - 基于两个组合框选择的过滤器表单
- ios - 在 UserDefaults 中保存 UISwitch 状态
- php - MySQLi插入查询正在执行但未进入数据库
- php - 从多个mysql表构建boostrap4 navmenu
- ubuntu - Tk 未在 tkinter ubuntu 中定义
- android - 启动画面:显示延迟且与预览不同
- google-chrome - 当用户离开时,IE 11(Edge)上的 sessionStorage 被清除
- c# - 文本框在错误的地方分割线
- c++ - 静态链接 cppunit 时如何解决“未定义的引用”错误?
- bash - Bash 脚本无法识别函数