打字稿 - 铸造字符串到自定义复杂对象

发布于 2025-01-31 07:10:47 字数 1438 浏览 2 评论 0原文

我有一个由自动化系统返回的字符串

"[
      {
        key: "FACTORY",
        prefix: () => h(NButton, { text: true, type: "primary" }, { default: () => "FACTORY" }),
        children: [
            {
              label: "id", key: "id",
              suffix: () => h(NButton, { text: true, type: "primary" }, { default: () => "marco" }),
            },
              {
                label: "groove", key: "groove",
                children: [
                    {
                      label: "shape", key: "shape",
                      suffix: () => h( NButton, { text: true, type: "primary" }, { default: () => "box" } ),
                    },
                ],
              }
        ],
      },
    ]"
  

填充 naive-ui> naive-ui ntree 组件。 Naive-UI组件需要 treeoption [] 类型才能正常运行。

export interface TreeOptionBase {
    key?: Key;
    label?: string;
    checkboxDisabled?: boolean;
    disabled?: boolean;
    isLeaf?: boolean;
    children?: TreeOption[];
    prefix?: () => VNodeChild;
    suffix?: () => VNodeChild;
}
export declare type TreeOption = TreeOptionBase & {
    [k: string]: unknown;
};

如何将我的生成的字符串转换为 treeoption [] 类型?

提前致谢

I have this string returned by an automated system.

"[
      {
        key: "FACTORY",
        prefix: () => h(NButton, { text: true, type: "primary" }, { default: () => "FACTORY" }),
        children: [
            {
              label: "id", key: "id",
              suffix: () => h(NButton, { text: true, type: "primary" }, { default: () => "marco" }),
            },
              {
                label: "groove", key: "groove",
                children: [
                    {
                      label: "shape", key: "shape",
                      suffix: () => h( NButton, { text: true, type: "primary" }, { default: () => "box" } ),
                    },
                ],
              }
        ],
      },
    ]"
  

It is a javascript object good to populate the Naive-UI NTree component.
The Naive-UI component require a TreeOption[] type to run fine.

export interface TreeOptionBase {
    key?: Key;
    label?: string;
    checkboxDisabled?: boolean;
    disabled?: boolean;
    isLeaf?: boolean;
    children?: TreeOption[];
    prefix?: () => VNodeChild;
    suffix?: () => VNodeChild;
}
export declare type TreeOption = TreeOptionBase & {
    [k: string]: unknown;
};

How to convert my generated string to TreeOption[] type ?

Thank's in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隔纱相望 2025-02-07 07:10:47

您可以尝试json.parse更改字符串 to array

You can try JSON.parse change String to Array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文