当前位置: 首页 > backend >正文

简单留插槽的方法

在封装组件的时候,很多组件里面的一些内容是不能完全写死的,所以需要留出插槽好让组件使用者自己定义展示形式,下面是很详细的组件留插槽的方法(用的Nuxt框架和TailWindCSS)

我在写项目的时候遇到了一个描述信息的组件,我把他命名为Description组件,这个组件的内容是这样的

<template><div class="w-full flex flex-col text-[#00303e]"><!-- 标题 --><div class="text-[2.5rem] mb-[1.5rem] text-[bold]">{{ props.title }}</div><!-- 描述信息 --><div><div class="text-[1.25rem] mb-[1.25rem] mt-[3rem] flex flex-col" v-for="item, index in props.descriptions":key="index"><div class="font-bold" v-if="item.title && item.title != ``">{{ item.title }}</div><div><span>{{ item.content }}</span><span class="font-bold">{{ item.stress }}</span></div><!-- 更多消息按钮 --><div v-if="item.path && item.path != ''" class="mt-[2rem]"><NuxtLink :to="item.path" class="cursor-pointer"><UButton class="pt-[16px] pb-[16px] pl-[20px] pr-[20px] text-[1.125rem]" color="neutral"variant="outline" :square="false">{{ item.button }}</UButton></NuxtLink></div></div></div></div>
</template>
<script setup>
const props = defineProps({title: {type: String,defalut: () => ``},descriptions: {type: Array,defalut: () => []}
})
</script>
<style scoped></style>

但是后来使用的时候发现内容部分有很多种形式,所以我想到给内容部分留一个插槽出来,可以让组件使用者自己选择内容部分是使用原来的默认展示还是用插槽自己定义展示方式,我在组件里面给添加了一个插槽:<slot name="descriptions"></slot>

<template><div class="w-full flex flex-col text-[#00303e]"><!-- 标题 --><div class="text-[2.5rem] mb-[1.5rem] text-[bold]">{{ props.title }}</div><!-- 描述信息 --><!-- 自定义的描述信息部分 --><div v-if="$slots.descriptions"><slot name="descriptions"></slot> <!-- 插槽:可以选择自定义描述信息 --></div><div v-else><div class="text-[1.25rem] mb-[1.25rem] mt-[3rem] flex flex-col" v-for="item, index in props.descriptions":key="index"><div class="font-bold" v-if="item.title && item.title != ``">{{ item.title }}</div><div><span>{{ item.content }}</span><span class="font-bold">{{ item.stress }}</span></div><!-- 更多消息按钮 --><div v-if="item.path && item.path != ''" class="mt-[2rem]"><NuxtLink :to="item.path" class="cursor-pointer"><UButton class="pt-[16px] pb-[16px] pl-[20px] pr-[20px] text-[1.125rem]" color="neutral"variant="outline" :square="false">{{ item.button }}</UButton></NuxtLink></div></div></div></div>
</template>
<script setup>
const props = defineProps({title: {type: String,defalut: () => ``},descriptions: {type: Array,defalut: () => []}
})
</script>
<style scoped></style>

在外部使用这个组件的话,应该这么写:<template v-slot:descriptions></template>注意这个descriptions一定要与组件里面定义的插槽名字一样才行

<!-- 这是不使用插槽的方式,内容部分按默认展示 -->
<Description :descriptions="aboutDescription" :title="`加强信息安全`"></Description><!-- 这是使用插槽的方式,内容部分就是自己在插槽里面自定义的展示方式 -->
<Description :title="`加强信息安全`"><!-- 注意这个插槽的名字一定要与组件里面插槽的名字一致 --><template v-slot:descriptions><div class="flex flex-col text-[1.25rem] text-[#00303e]"><div class=" mb-[1.25rem]" v-for="item, index in desArray" :key="index"><div class="mb-[1.25rem]">{{ item.content }}</div><ul v-if="item.child.length > 0" class="list-disc pl-[2.5rem] text-lg"><li v-for="value, index in item.child" :key="index"><div class="font-bold">{{ value.title }}</div><div>{{ value.content }}</div></li></ul></div></div></template>
</Description>

http://www.xdnf.cn/news/18495.html

相关文章:

  • 生成一个竖直放置的div,宽度是350px,上面是标题固定高度50px,下面是自适应高度的div,且有滚动条
  • 航空复杂壳体零件深孔检测方法 - 激光频率梳 3D 轮廓检测
  • FFMPEG相关解密,打水印,合并,推流,
  • 鸿蒙中Snapshot分析
  • Vue3+ElementPlus倒计时示例
  • 应用服务器和数据库服务器的区别
  • 机器学习案例——预测矿物类型(数据处理部分)
  • [CISCN2019 华北赛区 Day1 Web5]CyberPunk
  • `sudo apt update` 总是失败
  • Linux问答题:调优系统性能
  • 李宏毅NLP-12-语音分类
  • 基于Labview的旋转机械AI智能诊断系统
  • 2015-2018年咸海流域1km归一化植被指数8天合成数据集
  • html-docx-js 导出word
  • Linux问答题:归档和传输文件
  • 探秘北斗卫星导航系统(BDS):架构、应用与未来蓝图,展现中国力量
  • 文件系统挂载详细分析(《图解Linux内核》虚拟文件系统篇笔记二)
  • UDP报文的数据结构
  • 可转换债券高频交易Level-2五档Tick级分钟历史数据分析
  • 20250823解决荣品RD-RK3588-MID核心板的底板的adb不通
  • 超越基础:Glide 高级优化与自定义实战
  • 12.Shell脚本修炼手册--函数的基础认知与实战演练(fock炸弹!!)
  • 第1.2节:早期AI发展(1950-1980)
  • Mybatis Plus - 代码生成器简单使用
  • Baumer高防护相机如何通过YoloV8深度学习模型实现社交距离的检测识别(python)
  • 【204页PPT】某著名企业信息化规划方案(附下载方式)
  • 【攻防世界】Web_php_include
  • GitLab CI:安全扫描双雄 SAST vs. Dependency Scanning 该如何抉择?
  • 阿德莱德多模态大模型导航能力挑战赛!NavBench:多模态大语言模型在具身导航中的能力探索
  • C++ csignal库详细使用介绍