javascript - 如何在没有参数的情况下获得反应路由器原始网址?
问题描述
我想在没有路由器参数的情况下生成 url 路径。
// routers
<Route
exact
path={`/users/userDetail/:userId`}
component={UserDetail}
/>
i want to get string "/users/userDetail" from some components
帮我!
解决方案
如果我理解正确,您想提取当前路由的路径,同时排除userId
URL 的最后一部分 - 假设是这种情况,您可以执行以下操作:
const getCurrentPathWithoutLastPart = () => {
return location.pathname.slice(0, location.pathname.lastIndexOf('/'))
}
如果您当前的 URL 类似于/users/userDetail/some_value
调用该函数将产生/users/userDetail
:
getCurrentPathWithoutLastPart() // returns /users/userDetail
推荐阅读
- javascript - 捕获承诺拒绝
- sql-server - 如果列表 sql server 中不存在数据,则返回值
- java - 如何通过输出变量正确格式化,以使其与标题对齐?例如姓氏,用户中的名字等
- git - 源树软重置后丢失所有更改
- java - 如何将 PDDocument 的所有页面保存在单独的 .pdf 文件中?
- bit - Bitsrc:错误:标记组件时出现未知对象类型“窗口”
- java - 方法体内的 lambda 表达式
- php - 从 woocommerce 页面中删除重力形式动作
- java - 如何在自动化中找到类似于 getText() 的元素定位器
- angular - 在 ng2-charts 中隐藏三个工具提示之一