javascript React路由器v6 -未呈现

rsl1atfo  于 2023-02-07  发布在  Java
关注(0)|答案(2)|浏览(183)

我正在学习React,我不明白为什么react路由器v6没有渲染任何组件。

源代码/索引.js

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter, Routes, Route } from "react-router-dom";
import { Customers, Invoices, Jobs, Login, Sales, } from './modules'

import './App.css';
import './index.css';

import App from './App';

const rootElement = document.getElementById("root");
ReactDOM.render(
    <BrowserRouter>
        <Routes>
            <Route path="/" element={<App />}>
                <Route path="/jobs" element={<Jobs />} />
                <Route path="customers" element={<Customers />} />
                <Route path="invoices" element={<Invoices />} />
                <Route path="sales" element={<Sales />} />
                <Route
                path="*"
                element={
                    <main style={{ padding: "1rem" }}>
                    <p>There's nothing here!</p>
                    </main>
                }
                />
            </Route>
        </Routes>
    </BrowserRouter>,
    rootElement
);

源代码/模块/发票/发票.js

import { Link } from "react-router-dom";
import { getInvoices } from "../../data";

export default function Invoices() {
  let invoices = getInvoices();
  return (
    <div style={{ display: "flex" }}>
      <nav
        style={{
          borderRight: "solid 1px",
          padding: "1rem",
        }}
      >
        {invoices.map((invoice) => (
          <Link
            style={{ display: "block", margin: "1rem 0" }}
            to={`/invoices/${invoice.number}`}
            key={invoice.number}
          >
            {invoice.name}
          </Link>
        ))}
      </nav>
    </div>
  );
}

data.js取自React路由器文档https://reactrouter.com/docs/en/v6/getting-started/tutorial

我做错了什么?

lymnna71

lymnna711#

需要添加到组件中。我希望应用程序组件将b呈现,我们必须提到保持器,我们必须呈现子组件。

pkbketx9

pkbketx92#

ReactDOM.render(
    <BrowserRouter>
        <Routes>
            <Route path="/" element={<App />}/>
            <Route path="/jobs" element={<Jobs />} />
            <Route path="/customers" element={<Customers />} />
            <Route path="/invoices" element={<Invoices />} />
            <Route path="/sales" element={<Sales />} />
            <Route
                path="*"
                element={
                    <main style={{ padding: "1rem" }}>
                    <p>There's nothing here!</p>
                    </main>
                }
            />
       
        </Routes>
    </BrowserRouter>,
    rootElement
);

我发现了这些错误,
1.您已将所有组件包含在单个路由中。
或者如果你希望所有的东西都在你的组件里面。然后在应用程序组件本身初始化路由器。你可以参考这个https://github.com/NaveenkumarMD/cryptera-website/blob/master/src/App.js

相关问题