当前位置: 首页 > 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>

相关文章:

  • smss源代码分析之smss!SmpLoadSubSystemsForMuSession函数分析加载csrss.exe
  • yolov5 本地训练
  • Gradio全解20——Streaming:流式传输的多媒体应用(3)——实时语音识别技术
  • HBM的哪些事
  • 当LLM遇上Agent:AI三大流派的“复仇者联盟”
  • Linux操作系统系统编程:x86-64架构下的系统调用
  • Nature子刊:大脑如何灵活处理多元数字信息
  • Laravel 12 实现 API 登录令牌认证
  • 博弈论思维——AI与思维模型【90】
  • 书生实战营之沐曦专场
  • OceanBase租户扩缩容的三种方法
  • 2505C++,wmi客户端示例
  • 微软发布了最新的开源推理模型套件“Phi-4-Reasoning
  • C++ 项目中的多语言字符串管理方案(支持自动提示与动态加载)
  • 逻辑回归的多分类实战:以鸢尾花数据集为例
  • 【源码+文档+调试讲解】儿童图书推荐系统81
  • 论文笔记(八十三)STACKGEN: Generating Stable Structures from Silhouettes via Diffusion
  • C++负载均衡远程调用学习之QPS性能测试
  • 个人健康中枢的多元化AI软件革新与精准健康路径探析
  • 同城跑腿小程序帮取帮送接单抢单预约取件智能派单同城配送全开源运营版源码优创
  • “彩虹滑道”项目两男童相撞飞跌出去,景区:工作人员误判导致
  • 中央气象台:未来三天北方地区有大风沙尘,江南等地有强降水
  • 印尼巴厘岛多地停电,疑似海底电缆发生故障
  • 赵厚均评《唐诗与唐代园林景观的审美建构研究》|林泉恣探历,风景暂徘徊
  • “五一”看什么?这里有一份申城视听指南
  • “非思”的思想——探索失语者的思想史