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

组件通信-<slot>

目录

1. 默认插槽

2. 具名插槽

3. 作用域插槽

1. 默认插槽

父组件:

<template><div class="father"><h3>父组件</h3><div class="container"><Category title="今日游戏推荐"><ul><li v-for="item in games" :key='item.id'>{{ item.name }}</li></ul></Category><Category title="今日美食城市"><img :src="imageUrl" alt="" /></Category><Category title="今日影视推荐"><video :src="videoUrl" controls></video></Category></div></div>
</template><script setup lang="ts" name="Father">
import Category from './Category.vue'
import { ref, reactive } from 'vue'
let games = reactive([{ id: 'asgdytsa01', name: '英雄联盟' },{ id: 'asgdytsa02', name: '王者荣耀' },{ id: 'asgdytsa03', name: '红色警戒' },{ id: 'asgdytsa04', name: '斗罗大陆' }
])
let imageUrl = ref('https://img-blog.csdnimg.cn/direct/fb1e1f109889467a85eec6af0984611c.png')
let videoUrl = ref('https://media.w3.org/2010/05/sintel/trailer.mp4')
</script>
<style scoped>
.father {background-color: pink;padding: 20px;
}.container {display: flex;justify-content: space-evenly;
}img,
video {width: 100%;
}
</style>

子组件:

<template><div class="category"><h2>{{ title }}</h2><slot>默认内容</slot></div>
</template><script setup lang="ts" name="Category">
defineProps(['title'])
</script>
<style scoped>
.category {width: 200px;height: 300px;background-color: aquamarine;
}h2 {background-color: chartreuse;width: 100;text-align: center;
}
</style>

2. 具名插槽

<template><div class="father"><h3>父组件</h3><div class="container"><Category><template v-slot:s2><ul><li v-for="item in games" :key='item.id'>{{ item.name }}</li></ul></template><template v-slot:s1><h2>今日游戏推荐</h2></template></Category><Category><template v-slot:s2><img :src="imageUrl" alt="" /></template><template v-slot:s1><h2>今日图片推荐</h2></template></Category><Category><!-- 写法 v-slot:s2 也可以 #s2 --><template #s2><video :src="videoUrl" controls></video></template><template #s1><h2>今日影视推荐</h2></template></Category></div></div>
</template><script setup lang="ts" name="Father">
import Category from './Category.vue'
import { ref, reactive } from 'vue'
let games = reactive([{ id: 'asgdytsa01', name: '英雄联盟' },{ id: 'asgdytsa02', name: '王者荣耀' },{ id: 'asgdytsa03', name: '红色警戒' },{ id: 'asgdytsa04', name: '斗罗大陆' }
])
let imageUrl = ref('https://img-blog.csdnimg.cn/direct/fb1e1f109889467a85eec6af0984611c.png')
let videoUrl = ref('https://media.w3.org/2010/05/sintel/trailer.mp4')
</script>
<style scoped>
.father {background-color: pink;padding: 20px;
}.container {display: flex;justify-content: space-evenly;
}img,
video {width: 100%;
}h2 {background-color: chartreuse;width: 100;text-align: center;
}
</style>

子组件:

<template><div class="category"><h2>{{ title }}</h2><slot name="s1">默认内容</slot><slot name="s2">默认内容</slot></div>
</template><script setup lang="ts" name="Category">
defineProps(['title'])
</script>
<style scoped>
.category {width: 200px;height: 300px;background-color: aquamarine;
}</style>

3. 作用域插槽

  1. 理解:数据在组件的自身,但根据数据生成的结构需要组件的使用者来决定。(新闻数据在News组件中,但使用数据所遍历出来的结构由App组件决定)

  2. 具体编码:

父组件:

<template><div class="father"><h3>父组件</h3><div class="container"><!-- <Game v-slot:default="params"> --><!-- <Game #default="params"> --><Game v-slot="params"><ul><li v-for="g in params.games" :key="g.id">{{ g.name }}</li></ul></Game></div></div>
</template><script setup lang="ts" name="Father">
import Game from './Game.vue'</script>
<style scoped>
.father {background-color: pink;padding: 20px;
}.container {display: flex;justify-content: space-evenly;
}img,
video {width: 100%;
}h2 {background-color: chartreuse;width: 100;text-align: center;
}
</style>

子组件:

<template><div class="category"><h2>今日游戏榜单</h2><slot :games="games" a="哈哈"></slot></div>
</template><script setup lang="ts" name="Category">
import { reactive } from 'vue'
let games = reactive([{ id: 'asgdytsa01', name: '英雄联盟' },{ id: 'asgdytsa02', name: '王者荣耀' },{ id: 'asgdytsa03', name: '红色警戒' },{ id: 'asgdytsa04', name: '斗罗大陆' }
])
</script>
<style scoped>
.category {width: 200px;height: 300px;background-color: aquamarine;
}
</style>

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

相关文章:

  • SX24C01.UG-PXI程控电阻桥板卡
  • BLE协议栈的解析
  • 流水线相关计算【计算机组成与体系结构】
  • SpringTask
  • MySQL — 数据库建库与建表
  • html:table表格
  • B站Michale_ee——ESP32_IDF SDK——FreeRTOS_8 消息缓冲区
  • 神州趣味地名-基于天地图和LeafLet的趣味地名探索
  • 软件工程中的 QFD
  • 力扣面试150题--分隔链表
  • 深度学习视角下魔幻手机的实现探索与技术实践
  • python常用科学计算库及使用示例
  • 第六章 配置能力增强
  • C语言数据类型与内存布局
  • Linux系统中的用户分类、为什么Linux系统中有很多我没有创建的用户?
  • PyTorch_创建线性和随机张量
  • 数据中台笔记01
  • PaddleOCR移植到RK3568
  • 文章三《机器学习基础概念与框架实践》
  • 【STM32】定时器输入捕获
  • 怎么实现动态提示词,并提升准确率
  • [面试]SoC验证工程师面试常见问题(二)
  • ps将图标变清晰-cnblog
  • MATLAB绘制局部放大图
  • 【Bootstrap V4系列】 学习入门教程之 组件-警告框(Alert)
  • 【DecAlign】用于解耦多模态表征学习的分层跨模态对齐
  • Spring AI:简化人工智能功能应用程序开发
  • 对称加密算法(AES、ChaCha20和SM4)Python实现——密码学基础(Python出现No module named “Crypto” 解决方案)
  • mysql索引及数据库引擎
  • 计算方法实验三 解线性方程组的直接方法