默认插槽

使用方法

思路:

先编写好组件模板(子组件),然后在APP中传递数据给组件模板(父组件).

1.编写你要使用的组件模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<template>
<div id="Category">
<h3>{{ title }}分类</h3>
<!-- 定义插槽,默认插槽的名字是default -->
<slot>我是一些默认值,当使用者没有传递具体结构时,我会出现</slot>
</div>
</template>


<script>
export default {
name: "Category",
props:['listData','title','img','video']
};
</script>

<style scoped>
#Category {
background-color: skyblue;
width: 200px;
height: 300px;
}
h3 {
background-color: orange;
}
img {
width: 150px;
height: 100px;
}
video {
width: 100%;
}
</style>

2.编写APP组件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<template>
<div id="app">
<Category :listData="foods" title="美食">
<img :src="img" />
</Category>
<Category :listData="games" title="游戏"> </Category>
<Category :listData="films" title="电影">
<video :src="video" controls></video>
</Category>
</div>
</template>
<!-- ============================================== -->
<template>
<div id="app">
<Category :listData="foods" title="美食">
<ul>
<li v-for="(food, index) in foods" :key="index">{{ food }}</li>
</ul>
<img :src="img" />
</Category>
<Category :listData="games" title="游戏">
<ul>
<li v-for="(game, index) in games" :key="index">{{ game }}</li>
</ul>
</Category>
<Category :listData="films" title="电影">
<ul>
<li v-for="(film, index) in films" :key="index">{{ film }}</li>
</ul>
<video :src="video" controls></video>
</Category>
</div>
</template>