使用动态组件实现多个组件的过渡
简单实例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
<style>
button {
cursor: pointer;
}
.fade-enter,
.fade-leave-to {
opacity: 0;
}
.fade-enter-active,
.fade-leave-active {
transition: opacity 1s;
}
</style>
</head>
<body>
<div id="root">
<transition name="fade" mode="out-in"> //mode='out-in'先进入再隐藏
<component :is="type"></component>
</transition>
<button @click="handleClick">Taggle</button>
</div>
<script>
Vue.component('child', {
template: '<div>child</div>'
})
Vue.component('child-one', {
template: '<div>child-one</div>'
})
var vm = new Vue({
el: "#root",
data: {
type:"child"
},
methods: {
handleClick: function () {
this.type = (this.type==="child"?"child-one":"child")
}
},
})
</script>
</body>
</html>