Skip to content

Utils 工具类

VNodeUtil

isFragment

判断节点是否为 vue 片段(v-ifv-for 等指令片段)

isFragment(node: any): boolean

  • node: any - 目标节点

isComment

判断节点是否为注释

isComment(node: any): boolean

  • node: any - 目标节点

isValidElementNode

判断节点是否为有效的元素类型(非 vue 片段或注释)

isValidElementNode(node: any): boolean

  • node: any - 目标节点

getSlotChildren

获取指定插槽内部子元素节点

getSlotChildren(slots: Slots, name: string): VNode[]

  • slots: Slots - 组件插槽对象
  • name: string - 目标插槽名称
vue
<template>
  <div><slot></slot></div>
</template>

<script setup lang="ts">
  import { useSlots } from "vue";
  import { VNodeUtil } from "@easyui/element-plus-pro";

  const slots = useSlots();
  VNodeUtil.isFragment(slots.default());
  VNodeUtil.isComment(slots.default());
  VNodeUtil.isValidElementNode(slots.default());
  VNodeUtil.getSlotChildren(slots, "default");
</script>

parseClassStr

将 class 对象值解析为 class 字符串

parseClassStr(classObj: Record<string, boolean>): string

  • classObj: Record<string, boolean> - 目标 class 对象

parseClassObj

将 class 字符串值解析为 class 对象

parseClassObj(classStr: string): Record<string, boolean>

  • classStr: string - 目标 class 字符串

parseStyleStr

将 style 对象值解析为 style 字符串

parseStyleStr(styleObj: Record<string, any>): string

  • styleObj: Record<string, any> - 目标 style 对象

parseStyleObj

将 style 字符串值解析为 style 对象

parseStyleObj(styleStr: string): Record<string, any>

  • styleStr: string - 目标 style 字符串
vue
<script setup lang="ts">
  import { VNodeUtil } from "@easyui/element-plus-pro";

  VNodeUtil.parseClassStr({ "example-wrapper": true, "example-border": true });
  VNodeUtil.parseClassObj("example-wrapper example-border");
  VNodeUtil.parseStyleStr({ width: "50px", height: "50px" });
  VNodeUtil.parseStyleObj("width: 50px;height: 50px;");
</script>

NumberUtil

parseInt

将目标字符串转换为指定基数的整数

parseInt (str: string, defaultValue: number = 0, radix?: number): number

  • str: string - 要转换的字符串
  • defaultValue: number - 默认值
  • radix: number - 转换基数
vue
<script setup lang="ts">
  import { NumberUtil } from "@easyui/element-plus-pro";

  NumberUtil.parseInt("");       // 0
  NumberUtil.parseInt("", 1);    // 1
  NumberUtil.parseInt("10");       // 10
</script>