reactjs - ant design中如何获取当前URL的最后一部分
问题描述
我正在 ant design 中制作一个编辑表单,我想获取从组件中的以下路由传递的 id
{
path: '/post/edit/:id',
name: 'edit',
hideInMenu: true,
component: './Post/PostForm'
},
解决方案
我使用以下功能:
export function getLastPart() {
const parts = window.location.href.split('/');
return parts[parts.length - 1];
}
然后,在组件中,您需要检查最后一部分是否为实际 ID。例如,我使用 UUID,所以我也使用以下函数:
const uuidReg = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
export function isUuid(path) {
return uuidReg.test(path);
}
推荐阅读
- c# - 如何在 VB.Net 中编写 Lambda 属性
- android - 内部带有 MotionLayout 的 AppBarLayout android:layout_height="wrap_content" 不起作用
- python - Venv解释器比系统解释器快??蟒蛇 3.6
- android - 读取文档大小时的 Firestore 读取计数
- android - 需要在 Android 的 CameraX 中添加网格线
- python - sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) 没有这样的列
- apache-kafka - 我如何让 kafka 根据提供的保留期删除日志
- php - 使用 LEFT JOIN 多对多
- css - 你将如何优化这个 CSS?也许使用 SASS 或变量
- c# - 更新 mongocharpdriver nuget