如何在 vue.js 实现 tree 组件?

本文个人博客地址:https://www.leafage.top/posts/21328R4IT

在开发项目过程中,每个管理系统都需要权限、角色管理,这两个模块大都会使用 tree 型结构来展示数据,这样更加清晰明了,其实很多组件库都会包含自己创建的 tree 组件来供使用,但是这些组件库使用起来,会有很多需要根据需要去修改的地方。于是开始自己动手写一个定制化的 tree 组件

  1. 核心组件 tree-core
<template>
  <div class="my-1 px-2 hover:bg-gray-300 hover:bg-opacity-30 rounded-md">
    <div class="flex flex-1 items-center">
      <slot />
      <button
        v-if="data.children && data.children.length > 0"
        type="button"
        :name="data.name"
        :aria-label="data.name"
        :title="data.name"
        class="flex flex-1 items-center focus:outline-none"
        @click="$emit('open-operate')"
      >
        <svg
          v-if="data.expand && data.expand.icon"
          width="16"
          height="16"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="1.5"
          stroke-linecap="round"
          stroke-linejoin="round"
          class="mr-2"
        >
          <use :xlink:href="'/svg/feather-sprite.svg#' + data.expand.icon" />
        </svg>
        <span class="py-1  mr-auto">{{ data.name }}</span>
        <svg
          width="16"
          height="16"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          class="ml-4"
        >
          <use
            v-if="isExpand"
            :xlink:href="'/svg/feather-sprite.svg#' + 'chevron-down'"
          />
          <use
            v-else
            :xlink:href="'/svg/feather-sprite.svg#' + 'chevron-right'"
          />
        </svg>
      </button>
      <label
        v-else
        :for="data.code"
        class="flex flex-1 items-center cursor-pointer"
      >
        <svg
          v-if="data.expand && data.expand.icon"
          width="16"
          height="16"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="1.5"
          stroke-linecap="round"
          stroke-linejoin="round"
          class="mr-2"
        >
          <use :xlink:href="'/svg/feather-sprite.svg#' + data.expand.icon" />
        </svg>
        <span class="py-1 ">{{ data.name }}</span>
      </label>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { PropType } from "vue";

import type { NodeData } from "@/api/request.type";

defineProps({
  data: {
    type: Object as PropType<NodeData>,
    default: () => {
      return {}
    }
  },
  isExpand: {
    type: Boolean,
    default: false
  }
})

defineEmits(['open-operate'])
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值