The role of the slot:
Allowing the parent component to insert the html structure to the specified position of the child component is also a way of communication between components , which is suitable for parent components ==> child components ;
1. Default slot
Instructions:
①. In the parent component, write the label of the child component in the form of two nodes , and write the content of the label to be passed in in the middle ;
②. In the sub-component, use the <slot></slot> tag to occupy the place, and wait for the user to write code in the sub-component tag of the parent component , and the written code content will be filled in the position where the slot tag is located ;
Subcomponent (Category.vue) code:
<template>
<div class="category">
<h3>{
{title}}分类</h3>
<!-- 定义一个默认插槽,等待使用者编写标签进行填充 -->
<slot>如果没有设置插入的标签,我就会出现</slot>
</div>
</template>
Parent component (App.vue) code:
<template>
<div class="container">
<!-- 将子组件标签写成双节点形式,在中间写上要传入的标签内容 -->
<Category title="美食">
<img src="https://s3.ax1x.com/2021/01/16/srJ1q0.jpg" alt="">
</Category>
</div>
</template>
2. Named slots
Instructions:
①. In the child component, set the name attribute for the slot tag . The attribute name is the name of the slot , so that the parent component can locate each tag at the specified position ;
①. In the parent component, set the slot attribute for the label in the child component label , but only set the v-slot attribute for the template label ;
Category.vue code:
<template>
<div class="category">
<h3>{
{title}}分类</h3>
<!-- 具名插槽 -->
<slot name="center">如果没有设置插入的标签,我就会出现1</slot>
<slot name="footer">如果没有设置插入的标签,我就会出现2</slot>
</div>
</template>
App.vue code:
<template>
<div class="container">
<!-- 将组件标签写成双节点形式,在中间写上要传入的标签内容 -->
<Category title="美食">
<img slot="center" src="https://s3.ax1x.com/2021/01/16/srJ1q0.jpg" alt="">
<a slot="footer" href="http://www.baidu.com">更多美食</a>
</Category>
<Category title="游戏">
<ul slot="center">
<li v-for="(item,index) in games" :key="index">{
{item}}</li>
</ul>
<div class="foot" slot="footer">
<a href="http://www.baidu.com">单机游戏</a>
<a href="http://www.baidu.com">网络游戏</a>
</div>
</Category>
<Category title="电影">
<video slot="center" controls src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"></video>
<!-- v-slot属性只可以在template标签使用 -->
<template v-slot:footer>
<div class="foot">
<a href="http://www.baidu.com">经典</a>
<a href="http://www.baidu.com">热门</a>
<a href="http://www.baidu.com">推荐</a>
</div>
<h4>欢迎前来观影</h4>
</template>
</Category>
</div>
</template>
3. Scope slots
effect:
The data is in the child component itself , but the user of the parent component needs to decide according to the structure of the data generated in the parent component ;
Category.vue code:
<template>
<div class="category">
<h3>{
{title}}分类</h3>
<!-- 将games数据通过传参的方式传递给App -->
<slot :games="games"></slot>
</div>
</template>
<script>
export default {
name:'Category',
props:['title'],
data() {
return {
games:['王者荣耀','糖豆人','穿越火线']
}
}
}
</script>
App.vue code:
<template>
<div class="container">
<!-- 实现根据用户自定义标签 -->
<!-- 使用scope属性,属性值可以与组件插槽的属性名不同 -->
<Category title="游戏">
<!-- 一定要使用template标签包裹内容 -->
<template scope="atguigu">
<ul>
<li v-for="(item,index) in atguigu.games" :key="index">{
{item}}</li>
</ul>
</template>
</Category>
<Category title="游戏">
<template scope="atguigu">
<ol>
<li v-for="(item,index) in atguigu.games" :key="index">{
{item}}</li>
</ol>
</template>
</Category>
<Category title="游戏">
<template scope="atguigu">
<h4 v-for="(item,index) in atguigu.games" :key="index">{
{item}}</h4>
</template>
</Category>
</div>
</template>