// 子组件
<template>
<view class="card">
<view>我是子组件</view>
<view class="title" v-if="$slots.title">
<slot name="title"></slot>
</view>
<slot/>
<view class="footer" v-if="$slots.footer">
<slot name="footer"></slot>
</view>
</view>
</template>
<script>
export default {
}
</script>
<style lang="scss" scoped>
.card {
border: 2upx solid #000;
margin-bottom: 20upx;
.title {
color: red;
}
.footer {
color: blue;
}
}
</style>
// 父组件
<template>
<view>
<childComponent>
<template v-if="step === 1">
<view>我是步骤1的内容</view>
</template>
<template v-if="step === 2">
<view slot="title">我是步骤2的标题,我是红色的</view>
<view>我是步骤2的内容</view>
<view slot="footer">我是步骤2的FOOTER</view>
</template>
</childComponent>
</view>
</template>
<script>
import childComponent from './_components/childComponent.vue'
export default {
components: {
childComponent
},
data() {
return {
step: 1
}
},
mounted() {
setTimeout(() => {
this.step = 2;
}, 3000);
}
}
</script>
<style>
</style>
8***@qq.com (作者)
实际业务场景中,会出现如果需要判断是否存在插槽的情况,想到了另外一个方法是用v-show
2024-02-04 10:06