取消精准匹配

路由嵌套 - 图1

路由嵌套 - 图2

路由嵌套 - 图3

list.js

importReact, { Component } from‘react’ import { Link, Route } from‘react-router-dom’ importOuterfrom‘../components/outer’ importInnerfrom‘../components/inner’ classListextendsComponent { render(props) { console.log(this.props) return ( <div> <div> <p><Linkto=“/detail/1”>测试文章1</Link></p> <p><Linkto=“/detail/1”>测试文章2</Link></p> <p><Linkto=“/detail?title=’来自于list’”>测试文章1</Link></p> </div> <div> //一级路由名称变化后,二级路由中的名称动态变化 <Linkto={</font><font style="color:#569cd6;background-color:#000000;">${this</font><font style="color:#d4d4d4;background-color:#000000;">.</font><font style="color:#4fc1ff;background-color:#000000;">props</font><font style="color:#d4d4d4;background-color:#000000;">.</font><font style="color:#9cdcfe;background-color:#000000;">match</font><font style="color:#d4d4d4;background-color:#000000;">.</font><font style="color:#9cdcfe;background-color:#000000;">url</font><font style="color:#569cd6;background-color:#000000;">}</font><font style="color:#ce9178;background-color:#000000;">/inner}>国内新闻</Link> <Linkto={</font><font style="color:#569cd6;">${this</font><font style="color:#d4d4d4;">.</font><font style="color:#4fc1ff;">props</font><font style="color:#d4d4d4;">.</font><font style="color:#9cdcfe;">match</font><font style="color:#d4d4d4;">.</font><font style="color:#9cdcfe;">url</font><font style="color:#569cd6;">}</font><font style="color:#ce9178;">/outer}>国际新闻</Link> </div> <Routepath={</font><font style="color:#569cd6;background-color:#000000;">${this</font><font style="color:#d4d4d4;background-color:#000000;">.</font><font style="color:#4fc1ff;background-color:#000000;">props</font><font style="color:#d4d4d4;background-color:#000000;">.</font><font style="color:#9cdcfe;background-color:#000000;">match</font><font style="color:#d4d4d4;background-color:#000000;">.</font><font style="color:#9cdcfe;background-color:#000000;">path</font><font style="color:#569cd6;background-color:#000000;">}</font><font style="color:#ce9178;background-color:#000000;">/inner}component={Inner}/> <Routepath={</font><font style="color:#569cd6;">${this</font><font style="color:#d4d4d4;">.</font><font style="color:#4fc1ff;">props</font><font style="color:#d4d4d4;">.</font><font style="color:#9cdcfe;">match</font><font style="color:#d4d4d4;">.</font><font style="color:#9cdcfe;">path</font><font style="color:#569cd6;">}</font><font style="color:#ce9178;">/outer}component={Outer}/> </div> ) } } exportdefaultList

V6

嵌套路由的 path 可以不用写父级,会直接拼接; 嵌套路由必须在父级追加 Outlet 组件,作为子级组件的占位符,类似于 vue-router 中的 router-view

// Router.tsx

import React from “react”;

import { BrowserRouter, Route, Routes } from “react-router-dom”;

import Home from “../home/Home”;

import Goods from “../goods/Goods”;

import Customer from “../customer/Customer”;

import NotFound from “../not-found/NotFound”;

import GoodsDetail from “../goods/goods-detail/GoodsDetail”;

import GoodsList from “../goods/goods-list/GoodsList”;

export default function Router() {

return (

  1. <BrowserRouter>
  2. <Routes>
  3. <Route path='/' element={<Home />}>
  4. <Route path='goods' element={<Goods />} >
  5. {/* 动态路由 */}
  6. <Route path="<font style="color:#F5222D;">:id</font>" element={<GoodsDetail />}/>
  7. <Route path="list" element={<GoodsList />}/>
  8. </Route>
  9. <Route path='customer' element={<Customer />} ></Route>
  10. </Route>
  11. <Route path="*" element={<NotFound />} />
  12. </Routes>
  13. </BrowserRouter>

);

}

// Home.tsx

import React from “react”;

import { Outlet, Link } from “react-router-dom”;

export default function Home() {

return (

  1. <div>
  2. <h1>Home</h1>
  3. <p>
  4. <Link to='<font style="color:#F5222D;">/goods</font>'>to goods</Link>
  5. </p>
  6. <p>
  7. <Link to='/customer'>to customer</Link>
  8. </p>
  9. <font style="color:#F5222D;"><Outlet /></font>
  10. </div>

);

}

// Goods.tsx

import React from “react”;

import { useNavigate, Outlet } from “react-router-dom”;

export default function Goods() {

const navigate = useNavigate();

const handleClickToHome = () => {

  1. navigate("/");
  2. // history 的 replace 模式
  3. // navigate("/", { replace: true });

};

return (

  1. <div>
  2. <h2>Goods Page</h2>
  3. <button onClick={handleClickToHome}>to Home</button>
  4. <font style="color:#F5222D;"> {/* 子路由的占位组件 */}</font>
  1. </div>

);

}