取消精准匹配
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 (
<BrowserRouter>
<Routes>
<Route path='/' element={<Home />}>
<Route path='goods' element={<Goods />} >
{/* 动态路由 */}
<Route path="<font style="color:#F5222D;">:id</font>" element={<GoodsDetail />}/>
<Route path="list" element={<GoodsList />}/>
</Route>
<Route path='customer' element={<Customer />} ></Route>
</Route>
<Route path="*" element={<NotFound />} />
</Routes>
</BrowserRouter>
);
}
// Home.tsx
import React from “react”;
import { Outlet, Link } from “react-router-dom”;
export default function Home() {
return (
<div>
<h1>Home</h1>
<p>
<Link to='<font style="color:#F5222D;">/goods</font>'>to goods</Link>
</p>
<p>
<Link to='/customer'>to customer</Link>
</p>
<font style="color:#F5222D;"><Outlet /></font>
</div>
);
}
// Goods.tsx
import React from “react”;
import { useNavigate, Outlet } from “react-router-dom”;
export default function Goods() {
const navigate = useNavigate();
const handleClickToHome = () => {
navigate("/");
// history 的 replace 模式
// navigate("/", { replace: true });
};
return (
<div>
<h2>Goods Page</h2>
<button onClick={handleClickToHome}>to Home</button>
<font style="color:#F5222D;"> {/* 子路由的占位组件 */}</font>
</div>
);
}