写在最前:父组件引用子组件,如何能在应用子组件内容的同时,还能在自定义内容呢?
父组件中:
<MyContainer>
<div>html结构</div>
</MyContainer>
子组件中:
<template>
<div>
<!-- 定义插槽 -->
<slot>插槽默认内容</slot>
</div>
</template>
父组件中:
<MyContainer>
<template slot="content">
<div>html结构</div>
</template>
<template v-slot:content>
<div>html结构</div>
</template>
</MyContainer>
子组件中:
<template>
<div class="container">
<!-- 具名插槽 -->
<slot name="content">插槽默认内容</slot>
<slot name="footer">插槽默认内容</slot>
</div>
</template>
父组件:
<template slot-scope="list">
<h3 v-for="(item,index) in list.data" :key="index">{{ item }}</h3>
</template>
子组件:
<template>
<div>
<slot :data="fineList"></slot>
</div>
</template>
<script>
export default {
data(){
return {
fineList: [ 'node', 'yarn', 'npm', 'cnpm']
}
}
}
</script>
示例:准备两个组件:父组件App.vue
,子组件MyContainer.vue
1. 父组件App.vue
内:
2. 子组件MyContainer.vue
内:
3. 运行效果:
v-enter
:进入的起点v-enter-active
:进入过程中v-enter-to
: 进入的终点v-leave
:离开的起点v-leave-active
:离开过程中v-leave-to
: 离开的终点<transition>
包裹要过渡的元素,并配置name
属性<transition name="show">
<h1 v-show="flag">showContent</h1>
</transition>
<style>
.anmi-enter {
opacity: 0;
}
.anmi-enter-active {
transition: all 1s;
}
.anmi-enter-to {
opacity: 1;
}
.anmi-leave {
opacity: 1;
}
.anmi-leave-active {
transition: all 1s;
}
.anmi-leave-to {
opacity: 0;
}
</style>
<transition-group>
,且每个元素都有指定key
值<template>
<div>
<TransitionGroup name="anmi" tag="ul" class="list">
<li v-for="item in list" :key="item.id">
{{ item.name }}
<button @click="list.pop()">删除</button>
</li>
</TransitionGroup>
<button @click="list.push({ id: list.length, name: list.length + '小' })">
添加
</button>
</div>
</template>
<script>
export default {
data() {
return {
list: [
{ id: 0, name: "小明" },
{ id: 1, name: "小红" },
{ id: 2, name: "小白" },
],
};
},
};
</script>
<style>
.anmi-enter-active {
animation: move 1.5s;
}
.anmi-leave-active {
animation: move 1.5s reverse;
}
@keyframes move {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
</style>