普通插槽
<template>
<div class="sslot">
<h3>{{ title }}</h3>
<slot></slot>
</div>
</template>
<script>
export default {
props: ["title"],
};
</script>
<style lang="scss" scoped>
.sslot {
background-color: skyblue;
width: 200px;
height: 300px;
}
</style>
<template>
<div class="home">
<myson title="普通插槽">
<h2>插槽位置</h2>
</myson>
</div>
</template>
<script>
import myson from "@/components/son.vue";
export default {
name: "son",
components: {
myson,
},
};
</script>
<style lang="scss" scoped>
.home {
display: flex;
justify-content: space-around;
}
</style>
具名插槽
<template>
<div class="sslot">
<h3>{{ title }}</h3>
<slot name="first"></slot>
<slot name="second"></slot>
</div>
</template>
<script>
export default {
props: ["title"],
};
</script>
<style lang="scss" scoped>
.sslot {
background-color: skyblue;
width: 200px;
height: 300px;
}
</style>
<template>
<div class="home">
<myson title="具名插槽">
<template v-slot:first>
<h2>名字为first的插槽位置</h2>
</template>
<h2 slot="second">名字为second的插槽位置</h2>
</myson>
</div>
</template>
<script>
import myson from "@/components/son.vue";
export default {
name: "son",
components: {
myson,
},
};
</script>
<style lang="scss" scoped>
.home {
display: flex;
justify-content: space-around;
}
</style>
作用域插槽
<template>
<div class="sslot">
<h3>{{ title }}</h3>
<slot :dataOfMyson="dataOfMyson"></slot>
</div>
</template>
<script>
export default {
props: ["title"],
data() {
return {
dataOfMyson: "son组件的数据",
};
},
};
</script>
<style lang="scss" scoped>
.sslot {
background-color: skyblue;
width: 200px;
height: 300px;
}
</style>
<template>
<div class="home">
<myson title="作用域插槽">
<template v-slot="{dataOfMyson}">
<h2>插槽位置:{{ dataOfMyson }}</h2>
</template>
</myson>
</div>
</template>
<script>
import myson from "@/components/son.vue";
export default {
name: "son",
components: {
myson,
},
};
</script>
<style lang="scss" scoped>
.home {
display: flex;
justify-content: space-around;
}
</style>
|