1、默认插槽
直接在组件中写结构,传入组件中
<Category title="美食">
<img
src="https://img1.baidu.com/it/u=3003020177,2948003422&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"
alt="">
</Category>
<Category title="游戏">
<ul>
<li v-for="(item,index) in games" :key="index">{{item}}</li>
</ul>
</Category>
<Category title="电影">
<meta name=referrer content=no-referrer>
<video controls src="http://vfx.mtime.cn/Video/2019/03/12/mp4/190312083533415853.mp4"></video>
</Category>
< slot>标签
<template>
<div class="category">
<h3>{{title}}分类</h3>
<!-- 挖个坑,等着组件的使用者进行填充 -->
<slot>默认值,当使用者没有传递具体结构时,会显示</slot>
</div>
</template>
category组件
<template>
<div class="category">
<h3>{{title}}分类</h3>
<!-- 挖个坑,等着组件的使用者进行填充 -->
<slot>默认值,当使用者没有传递具体结构时,会显示</slot>
</div>
</template>
<script>
export default{
name:'Category',
props:['title']
}
</script>
<style scoped>
.category{
background-color: skyblue;
width:200px;
height: 300px;
}
h3{
text-align: center;
background-color: orange;
}
img{
width: 100%;
}
</style>
app组件
<template>
<div class="app">
<h1>{{msg}}</h1>
<School :getSchoolName="getSchoolName"/>
<!-- <Student v-on:atguigu="getStudentName"/> -->
<Student ref="student"/>
<!-- vue中借助ruoter-link 标签实现路由的切换 -->
<!-- <router-link active-class="active" to='/about'>About</router-link> -->
<router-link active-class="active" :to="{name:'guanyu'}">About</router-link>
<br/>
<router-link active-class="active" to="/home">Home</router-link>
<div class="router_view">
<router-view></router-view>
</div>
<button @click="getHome">获取主页信息</button>
<div class="container">
<Category title="美食">
<img
src="https://img1.baidu.com/it/u=3003020177,2948003422&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"
alt="">
</Category>
<Category title="游戏">
<ul>
<li v-for="(item,index) in games" :key="index">{{item}}</li>
</ul>
</Category>
<Category title="电影">
<meta name=referrer content=no-referrer>
<video controls src="http://vfx.mtime.cn/Video/2019/03/12/mp4/190312083533415853.mp4"></video>
</Category>
</div>
</div>
</template>
<script>
import School from './components/School'
import Student from './components/Student'
import axios from 'axios'
import Category from './components/Category'
export default {
name: 'App',
components: {
School,
Student,
Category
},
data(){
return{
msg:'你好!',
foods:['火锅','烧烤','小龙虾','牛排'],
games:['红色警戒','穿越火线','劲舞团','超级玛丽'],
films:['《教父》','《拆弹专家》','《你好,李焕英》','《猪猪侠》'],
}
},
methods:{
getSchoolName(name){
console.log('app收到了schoole的数据:',name)
},
getStudentName(name){
console.log('app收到了student的数据:',name)
},
demo(){
console.log('demo绑定')
},
getHome(){
axios.get('http://localhost:8080/proxy/home/getHome').then(
response => {
console.log('请求成功了',response.data)
},
error => {'请求失败了',error.msg}
)
}
},
mounted(){
this.$refs.student.$on('atguigu',this.getStudentName)
this.$refs.student.$on('demo',this.demo)
}
}
</script>
<style>
.app{
background-color: bisque;
}
.router_view{
background-color: aliceblue;
}
.container{
display: flex;
justify-content: space-around;
}
video{
width: 100%;
}
</style>
2、具名插槽
当使用多个插槽时,想给不同位置,分别添加不同的结构
<template>
<div class="category">
<h3>{{title}}分类</h3>
<!-- 挖个坑,等着组件的使用者进行填充 -->
<slot name="center">默认值,当使用者没有传递具体结构时,会显示1</slot>
<slot name="footer">默认值,当使用者没有传递具体结构时,会显示2</slot>
</div>
</template>
<div class="container">
<Category title="美食">
<img
slot="center"
src="https://img1.baidu.com/it/u=3003020177,2948003422&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"
alt
/>
<a slot="footer" href>更多美食</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>单机游戏</a>
<a href>网络游戏游戏</a>
</div>
</Category>
<Category title="电影">
<meta name="referrer" content="no-referrer" />
<video
slot="center"
controls
src="http://vfx.mtime.cn/Video/2019/03/12/mp4/190312083533415853.mp4"
></video>
<!-- <template slot="footer"> -->
<!-- 当为template时,可以有第二种写法 -->
<template v-slot:footer>
<div class="foot">
<a href>经典</a>
<a href>热门</a>
<a href>推荐</a>
</div>
<h4>欢迎前来观影</h4>
</template>
</Category>
</div>
category组件
<template>
<div class="category">
<h3>{{title}}分类</h3>
<!-- 挖个坑,等着组件的使用者进行填充 -->
<slot name="center">默认值,当使用者没有传递具体结构时,会显示1</slot>
<slot name="footer">默认值,当使用者没有传递具体结构时,会显示2</slot>
</div>
</template>
<script>
export default{
name:'Category',
props:['title']
}
</script>
<style scoped>
.category{
background-color: skyblue;
width:200px;
height: 300px;
}
h3{
text-align: center;
background-color: orange;
}
img{
width: 100%;
}
</style>
app组件:
<template>
<div class="app">
<h1>{{msg}}</h1>
<School :getSchoolName="getSchoolName" />
<!-- <Student v-on:atguigu="getStudentName"/> -->
<Student ref="student" />
<!-- vue中借助ruoter-link 标签实现路由的切换 -->
<!-- <router-link active-class="active" to='/about'>About</router-link> -->
<router-link active-class="active" :to="{name:'guanyu'}">About</router-link>
<br />
<router-link active-class="active" to="/home">Home</router-link>
<div class="router_view">
<router-view></router-view>
</div>
<button @click="getHome">获取主页信息</button>
<div class="container">
<Category title="美食">
<img
slot="center"
src="https://img1.baidu.com/it/u=3003020177,2948003422&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"
alt
/>
<a slot="footer" href>更多美食</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>单机游戏</a>
<a href>网络游戏游戏</a>
</div>
</Category>
<Category title="电影">
<meta name="referrer" content="no-referrer" />
<video
slot="center"
controls
src="http://vfx.mtime.cn/Video/2019/03/12/mp4/190312083533415853.mp4"
></video>
<!-- <template slot="footer"> -->
<!-- 当为template时,可以有第二种写法 -->
<template v-slot:footer>
<div class="foot">
<a href>经典</a>
<a href>热门</a>
<a href>推荐</a>
</div>
<h4>欢迎前来观影</h4>
</template>
</Category>
</div>
</div>
</template>
<script>
import School from "./components/School";
import Student from "./components/Student";
import axios from "axios";
import Category from "./components/Category";
export default {
name: "App",
components: {
School,
Student,
Category,
},
data() {
return {
msg: "你好!",
foods: ["火锅", "烧烤", "小龙虾", "牛排"],
games: ["红色警戒", "穿越火线", "劲舞团", "超级玛丽"],
films: ["《教父》", "《拆弹专家》", "《你好,李焕英》", "《猪猪侠》"],
};
},
methods: {
getSchoolName(name) {
console.log("app收到了schoole的数据:", name);
},
getStudentName(name) {
console.log("app收到了student的数据:", name);
},
demo() {
console.log("demo绑定");
},
getHome() {
axios.get("http://localhost:8080/proxy/home/getHome").then(
(response) => {
console.log("请求成功了", response.data);
},
(error) => {
"请求失败了", error.msg;
}
);
},
},
mounted() {
this.$refs.student.$on("atguigu", this.getStudentName);
this.$refs.student.$on("demo", this.demo);
},
};
</script>
<style>
.app {
background-color: bisque;
}
.router_view {
background-color: aliceblue;
}
.container,
.foot {
display: flex;
justify-content: space-around;
}
video {
width: 100%;
}
</style>
3、作用域插槽
当数据在子组件中,但父组件要生成不同结构,通过插槽传递给子组件,可使用作用域插槽传递数据!
<template>
<div class="category02">
<h3>{{title}}分类</h3>
<!-- 挖个坑,等着组件的使用者进行填充 -->
<slot :games02="games02">当没有数据时的默认内容</slot>
</div>
</template>
<div class="container">
<Category02 title="游戏">
<template scope="category02Games">
<ul>
<li v-for="(g,index) in category02Games.games02" :key="index">{{g}}</li>
</ul>
</template>
</Category02>
<Category02 title="游戏">
<template scope="category02Games">
<ol>
<li v-for="(g,index) in category02Games.games02" :key="index">{{g}}</li>
</ol>
</template>
</Category02>
<Category02 title="游戏">
<template scope="category02Games">
<h4 v-for="(g,index) in category02Games.games02" :key="index">{{g}}</h4>
</template>
</Category02>
</div>
作用域插槽一定要使用标签template,才能传递数据!!
category02组件:
<template>
<div class="category02">
<h3>{{title}}分类</h3>
<!-- 挖个坑,等着组件的使用者进行填充 -->
<slot :games02="games02">当没有数据时的默认内容</slot>
</div>
</template>
<script>
export default{
name:'Category02',
props:['title'],
data() {
return {
games02: ["红色警戒", "穿越火线", "劲舞团", "超级玛丽"]
}
},
}
</script>
<style scoped>
.category02{
background-color: skyblue;
width:200px;
height: 300px;
}
h3{
text-align: center;
background-color: orange;
}
img{
width: 100%;
}
</style>
app组件:
<template>
<div class="app">
<h1>{{msg}}</h1>
<School :getSchoolName="getSchoolName" />
<!-- <Student v-on:atguigu="getStudentName"/> -->
<Student ref="student" />
<!-- vue中借助ruoter-link 标签实现路由的切换 -->
<!-- <router-link active-class="active" to='/about'>About</router-link> -->
<router-link active-class="active" :to="{name:'guanyu'}">About</router-link>
<br />
<router-link active-class="active" to="/home">Home</router-link>
<div class="router_view">
<router-view></router-view>
</div>
<button @click="getHome">获取主页信息</button>
<div class="container">
<Category title="美食">
<img
slot="center"
src="https://img1.baidu.com/it/u=3003020177,2948003422&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"
alt
/>
<a slot="footer" href>更多美食</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>单机游戏</a>
<a href>网络游戏游戏</a>
</div>
</Category>
<Category title="电影">
<meta name="referrer" content="no-referrer" />
<video
slot="center"
controls
src="http://vfx.mtime.cn/Video/2019/03/12/mp4/190312083533415853.mp4"
></video>
<!-- <template slot="footer"> -->
<!-- 当为template时,可以有第二种写法 -->
<template v-slot:footer>
<div class="foot">
<a href>经典</a>
<a href>热门</a>
<a href>推荐</a>
</div>
<h4>欢迎前来观影</h4>
</template>
</Category>
</div>
<h1>作用域插槽</h1>
<div class="container">
<Category02 title="游戏">
<template scope="category02Games">
<ul>
<li v-for="(g,index) in category02Games.games02" :key="index">{{g}}</li>
</ul>
</template>
</Category02>
<Category02 title="游戏">
<template scope="category02Games">
<ol>
<li v-for="(g,index) in category02Games.games02" :key="index">{{g}}</li>
</ol>
</template>
</Category02>
<Category02 title="游戏">
<template scope="category02Games">
<h4 v-for="(g,index) in category02Games.games02" :key="index">{{g}}</h4>
</template>
</Category02>
</div>
</div>
</template>
<script>
import School from "./components/School";
import Student from "./components/Student";
import axios from "axios";
import Category from "./components/Category";
import Category02 from "./components/Category02";
export default {
name: "App",
components: {
School,
Student,
Category,
Category02,
},
data() {
return {
msg: "你好!",
foods: ["火锅", "烧烤", "小龙虾", "牛排"],
games: ["红色警戒", "穿越火线", "劲舞团", "超级玛丽"],
films: ["《教父》", "《拆弹专家》", "《你好,李焕英》", "《猪猪侠》"],
};
},
methods: {
getSchoolName(name) {
console.log("app收到了schoole的数据:", name);
},
getStudentName(name) {
console.log("app收到了student的数据:", name);
},
demo() {
console.log("demo绑定");
},
getHome() {
axios.get("http://localhost:8080/proxy/home/getHome").then(
(response) => {
console.log("请求成功了", response.data);
},
(error) => {
"请求失败了", error.msg;
}
);
},
},
mounted() {
this.$refs.student.$on("atguigu", this.getStudentName);
this.$refs.student.$on("demo", this.demo);
},
};
</script>
<style>
.app {
background-color: bisque;
}
.router_view {
background-color: aliceblue;
}
.container,
.foot {
display: flex;
justify-content: space-around;
}
video {
width: 100%;
}
</style>
结果: