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

Vue学习记录之九(插槽slot)

插槽: 就是子组件中供给父组件提供一个占位符,父组件可以在这个占位符填充任何模版代码,如HTML,组件等。这些内容将子组件标签取代。
如A.vue 子组件

// 占位符,可以有多个插槽
<slot></slot>

父组件app.vue

<template>
<div>
	//必须在子组件<A></A> 之间写填充代码
	//填充代码必须使用<template v-slot>...</template>
	//而且在A中可以有多个<template v-slot>...</template>来填充子类的多个插槽。
	<A>
		<template v-slot>准备往子组件中添加的内容</template>
	</A>
</div>
</template>
<script lang='ts' setup>
import A from './component/A.vue'
</script>

插槽分: 匿名插槽具名插槽作用域插槽,和动态插槽

一、匿名插槽

子组件放置一个插槽 这里也没有使用name属性。 而且在父组件填充的位置
准备往子组件中添加的内容 的v-slot后面也没有要填充具体某个插槽,这样的插槽就是匿名插槽。 就上上面的例子一样。
在子组件的slot没有名称,所有叫匿名。

<slot></slot>

而父组件填充的内容中v-slot也有名称,这种就被称为具名插槽。

<template v-slot>准备往子组件中添加的内容</template>

二、具名插槽

在子组件的slot有name属性:有名称,所有叫具名

<slot name="header"></slot>

而父组件填充的内容中v-slot也有名称,才能插入对应的插槽。

<template v-slot:header>准备往子组件中添加的内容</template>

v-solot: 可以简写为 # , 上面和下面等同

<template #header>准备往子组件中添加的内容</template>

三、作用域插槽

在父组件可以拿到子组件的值,子组件在标签内的template通过属性来传递,父组件直接在slot=“{参数1,参数2}” 来接收。

1、父组件代码

<template>
    <div class="content">
      <Dialog>
      	<!--
			简写:<template #header>
		-->
        <template v-slot:header>
          <div>
            我是父类的header
          </div>
        </template>
        <!--
          v-slot="{data}"  表示要接受子类传递过来的data数据。
          v-slot="{data,index}"  可以简写为 #default = "{data,index}" 
        -->
        <template v-slot="{data,index}">
          <div>
            {{ data }}--{{ index }}
          </div>
        </template>
        <!--
			简写:<template #footer>
		-->
        <template v-slot:footer>
          <div>
            我是父类的footer
          </div>
        </template>
      </Dialog>
    </div>
</template>
<script setup lang='ts'>
import { ref,reactive } from 'vue'
import Dialog from './components/Dialog/index.vue'
</script>

2、子组件代码

<template>
    <div>
        <header class="header">
            <slot name="header"></slot>
        </header>
        <main class="main">
            <div v-for="(item,index) in data">
                <!--这里data名称可以随便写,但是父类接受需要这个名称,可以传递多个参数-->
                <slot :data="item"  :index="index"></slot>
            </div>
        </main>
        <footer class="footer">
            <slot name="footer"></slot>
        </footer>
    </div>
</template>
<script setup lang='ts'>
import { ref,reactive } from 'vue'
type names = {
    name:string,
    age:number
}
const data = reactive<names[]>([
    {
    name:'lvmanba1',
    age:10
    },
    {
    name:'lvmanba2',
    age:20
    },
    {
    name:'lvmanba3',
    age:30
    },
    {
    name:'lvmanba4',
    age:40
    },
])
</script>

四、动态插槽

父类中填充子类的具名插槽来自由于父类的对变量的赋值。

<template>
    <div class="content">
      <Dialog>
        <template #[name]>
          <div>我的位置决定于父类的变量的值</div>
        </template>
      </Dialog>
    </div>
</template>
<script setup lang='ts'>
import { ref,reactive } from 'vue'
import Dialog from './components/Dialog/index.vue'
const name = ref("footer")
</script>

http://www.kler.cn/news/318008.html

相关文章:

  • C/C++面试题
  • MySQL的msi版本9.0在安装过程总结和需要注意的地方
  • YOLOX预测图片是无法保存
  • 【AI小项目5】使用 KerasNLP 对 Gemma 模型进行 LoRA 微调
  • 【工具类】——图片缩放
  • 掌握Python办公自动化,轻松成为职场高效达人
  • CSS中的root的作用
  • 软件测试面试八股文(含文档)
  • 若依框架下项目,通过Nginx进行双服务部署
  • 使用k8s搭建mariadb+nginx+wordpress
  • JAVA毕业设计181—基于Java+Springboot+vue3的潮鞋交易管理系统(源代码+数据库+开题+万字论文)
  • 《华为三台交换机堆叠的详细命令行配置示例》
  • 观察者模式(发布-订阅模式)
  • Moshi: a speech-text foundation model for real time dialogue
  • 828华为云征文 | 解锁企业级邮件服务,在华为云Flexus x实例上部署Mailcow开源方案
  • ONNX模型部署利器ONNXRUNTIME框架
  • 算法-Init
  • [sniffer]分析
  • Excel 冻结多行多列
  • Python机器学习基础(NumPy、Pandas、Matplotlib)
  • 【CPP11?】结合CPP发展历史来理解CPP11
  • 代码随想录算法训练营Day14 | 226.翻转二叉树、101. 对称二叉树、104.二叉树的最大深度、111.二叉树的最小深度
  • 【MySQL内置数据库】information_schema
  • 【C++】检测TCP链接超时——时间轮组件设计
  • 自学前端的正确姿势是...
  • 第17周 第2章Session与ServletContext原理 ---ServletContext与三大作用域对象
  • PerparedStatement概述
  • Qt 模型视图(三):视图类QAbstractItemView
  • Python常见Json对比库deepdiff、json_tools、jsonpatch
  • 【Python】curl命令、Api POST导入cURL、python直接使用cURL