1

我正在 ant design 中制作一个编辑表单,我想获取从组件中的以下路由传递的 id

      {
        path: '/post/edit/:id',
        name: 'edit',
        hideInMenu: true,
        component: './Post/PostForm'
      },
4

1 回答 1

1

我使用以下功能:

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);
}
于 2018-10-05T10:16:43.800 回答