【发布时间】:2020-12-10 21:09:33
【问题描述】:
如何使用 react router 附加到路由?
假设当前路由是/page1/page2,我想路由到/page1/page2/next-page
首先我得到路由器
const router = useHistory();
当我像这样使用push 时
router.push("next-page");
它路由到/page1/next-page。
当我使用像这样添加/
router.push("/next-page");
它路由到/next-page
我也尝试过类似的方法
router.push(`${router.location.pathname}/next-page`)
但是这样的问题是,当我目前在/page1/page2/ 时,我最终在/page1/page2//next-page 有两个//。
有没有像router.push("/page1/page2/next-page")这样写完整路由的好方法?
【问题讨论】:
标签: reactjs react-router react-router-dom