第一步嵌套路由
{
path: "/shopping/goods",
element: <ShoppingGoods />,
children: [{ path: "/shopping/goods/detail", element: <GoodsDetail /> }],
},
第二步监听路由(点击查看详情时需要保存当前滚动条高度)
{
title: "操作",
key: "operation",
width: 100,
render: (text) => (
<span
className="ac"
onClick={() => {
navigate(`/shopping/goods/detail?goodsNo=${text.goodsNo}`);
setScrollTop(document.getElementById("code").scrollTop)
}}
>
跳转商品详情
</span>
),
},
const { pathname } = useLocation();
useEffect(() => {
if(pathname === "/shopping/goods") {
document.getElementById("code").scrollTop = scrollTop
}
}, [pathname])
第三步通过当前路径判断 展示list 页 还是 detail页面(Outlet 是react-router-dom中的内容 相当于router-view)