这里是区别:V5 vs V6
这里是官网:可以查看更多高级属性
src/routes/index
import React from "react";
const Home = React.lazy(() => import("../Pages/Home"));
const About = React.lazy(() => import("../Pages/About"));
const routes = [
{
path: "/home",
name: "home",
element: <Home></Home>,
},
{
path: "/about",
name: "about",
element: <About></About>,
},
];
export default routes
src / App.js
useRoutes可以代替标签完成:
import { useRoutes } from "react-router";
import routes from "./routes";
function App() {
const elements = useRoutes(routes);
return (
<div className="App">
<h3>{导航区...} </h3>
{elements}
</div>
);
}
export default App;
src / main.js
注意:
<BrowserRouter>
必须存在,包裹最外层奥
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<BrowserRouter>
<App />
</BrowserRouter>
);