组件封装-List

对于数组对象渲染列表的组件进行渲染,可支持定制里面的内容与列表外部的内容。

子组件

html 复制代码
<template>
  <view>
    <view v-for="item in data" :key="item.id" class="check-list-item">
      <nut-checkbox v-if="showCheckbox" :label="item.supplierId" class="w-full m_checkbox">
        <slot name="item" :item="item"></slot>
      </nut-checkbox>
      <view v-else>
        <slot name="item" :item="item"></slot>
      </view>
    </view>
  </view>
</template>
<script setup lang="ts" generic="T extends Record<string, any> & { id: any }">
import { PropType, defineSlots } from 'vue';

defineProps({
  data: {
    // eslint-disable-next-line no-undef
    type: Array as PropType<T[]>,
    required: true
  },
 // 根据条件定制列表外内容
  showCheckbox: {
    type: Boolean,
    default: false
  },
  height: {
    type: [String, Number],
    default: ''
  },
  loading: {
    type: Boolean,
    default: false
  }
});

defineSlots<{
  // eslint-disable-next-line
  item(_props: { item: T }): any;
}>();
</script>

<style lang="scss">
.check-list-box {
  overflow-y: scroll;

  .check-list-item {
    // box-shadow: 2px 2px 8px 0 #9999991a;
    border-radius: 4px;
  }
}
</style>

父组件

html 复制代码
  <CheckList
        v-model:selected="selected"
        class="mt-[16px]"
        :data="detail?.bargainSupplierVos || []"
        :loading="loading"
        :height="`calc(100vh - 236px - env(safe-area-inset-top))`"
        :show-checkbox="status === 'fixed-point'"
      >
    <!-- 定制列表内部的内容 -->
        <template #item="{ item }">
          <view @click="(e) => goDetail(e, item)">
    <!-- 封装内容的每一项的组件 -->
            <QuoteCard :data="item" :currency="Currency[detail?.briefProcureReq?.currency as any] || 'CNY'" />
          </view>
        </template>
      </CheckList>
相关推荐
灵感__idea5 小时前
Hello 算法:“走一步看一步”的智慧
前端·javascript·算法
Mh7 小时前
我决定写一个 3D 地球仪来记录下我要去的地方
前端·javascript·动效
. . . . .9 小时前
ref、useRef 和 forwardRef
前端·javascript·react.js
柚子8169 小时前
break跳出语句块的神奇技巧
前端·javascript
ejinxian10 小时前
Rust GUI框架Azul与Electron、WebView2
前端·javascript·electron
gumichef12 小时前
算法的时间复杂度和空间复杂度
数据结构
代码不加糖12 小时前
0基础搭建前后端分离项目:实现菜单与界面左右布局
java·前端·javascript·mysql·elementui·mybatis
zhensherlock12 小时前
Protocol Launcher 系列:Tally 快速计数器的深度集成
前端·javascript·typescript·node.js·自动化·github·js
cpp_250112 小时前
P1832 A+B Problem(再升级)
数据结构·c++·算法·动态规划·题解·洛谷·背包dp
AC赳赳老秦12 小时前
OpenClaw权限管理实操:团队共享Agent,设置操作权限,保障数据安全
服务器·开发语言·前端·javascript·excel·deepseek·openclaw