反应路由问题(无创建反应应用)

发布于 2025-02-11 07:22:16 字数 2228 浏览 0 评论 0原文

我已经创建了不使用CRA命令的React应用程序,还安装了React Router Router DOM V6。

当我在URL中有1个路径时,我不会遇到任何错误(例如http:// localhost:3000 /first path)。但是,当我在URL中有2个路径(例如http:// localhost:3000 /first path/secondpath)时,找不到404 first path/main.js。它正在搜索一个称为firstPath的文件夹中的main.js。

在以下代码中,索引,添加和测试URL正在工作。但是编辑URL不起作用。

console log图像 sources tab映像图像

rootRouter.js代码:[browserrouter tag在父母文件中给出了webpack]

import React from "react";
import { Route, Routes } from "react-router-dom";
import Home from "../views/Home";
import Add from "../views/Add";
import Edit from "../views/Edit";

const rootRouter = () => {
    return (
        <Routes>
            <Route exact path="/" element={<Home />} />
            <Route path="add" element={<Add />} />
            <Route path="edit/:id" element={<Edit />} />
            <Route path="test" element={<div>test</div>} />
        </Routes>
    );
};

export default rootRouter;

webpack。 config.js代码:

const path = require("path");
const port = process.env.PORT || 3000;

module.exports = {
    mode: "development",
    entry: "./index.js",
    output: {
        path: path.resolve(__dirname, "dist"),
        filename: "main.js",
    },
    target: "web",
    devServer: {
        host: "localhost",
        port: port,
        static: ["./public"],
        open: true,
        hot: true,
        liveReload: true,
        historyApiFallback: true,
    },
    resolve: {
        extensions: [".js", ".jsx", ".json", ".ts", ".tsx"],
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: ["babel-loader"],
            },
            {
                test: /\.css$/i,
                use: ["style-loader", "css-loader"],
            },
            {
                test: /\.png?$/,
                use: ["file-loader"],
            },
        ],
    },
};

I have created react app without using CRA command and also installed react router dom v6.

When I have 1 path in url, I don't get any error (like http://localhost:3000 /firstPath ). But, when I have 2 paths in URL (like http://localhost:3000 /firstPath/secondPath ), I get 404 firstPath/main.js not found error. It is searching for main.js inside a folder called firstPath.

In the following code, the index, add and test URLs are working. But the edit URL is not working.

Console Log Image
Sources Tab Image

rootRouter.js code: [BrowserRouter tag is given in the parent file]

import React from "react";
import { Route, Routes } from "react-router-dom";
import Home from "../views/Home";
import Add from "../views/Add";
import Edit from "../views/Edit";

const rootRouter = () => {
    return (
        <Routes>
            <Route exact path="/" element={<Home />} />
            <Route path="add" element={<Add />} />
            <Route path="edit/:id" element={<Edit />} />
            <Route path="test" element={<div>test</div>} />
        </Routes>
    );
};

export default rootRouter;

webpack.config.js code:

const path = require("path");
const port = process.env.PORT || 3000;

module.exports = {
    mode: "development",
    entry: "./index.js",
    output: {
        path: path.resolve(__dirname, "dist"),
        filename: "main.js",
    },
    target: "web",
    devServer: {
        host: "localhost",
        port: port,
        static: ["./public"],
        open: true,
        hot: true,
        liveReload: true,
        historyApiFallback: true,
    },
    resolve: {
        extensions: [".js", ".jsx", ".json", ".ts", ".tsx"],
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: ["babel-loader"],
            },
            {
                test: /\.css$/i,
                use: ["style-loader", "css-loader"],
            },
            {
                test: /\.png?$/,
                use: ["file-loader"],
            },
        ],
    },
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文