<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../js/vue.js" type="text/javascript"></script>
</head>
<style>
.fade-enter ,
.fade-leave-to{
opacity: 0;
}
.fade-enter-active ,
.fade-leave-active{
transition: opacity 3s;
}
</style>
<body>
<div id="app">
<transition name="fade" mode="out-in">
<component :is="type"></component>
</transition>
<button @click="change">点击</button>
</div>
</body>
<script>
Vue.component('child-one',{
template:'<div>child-one</div>',
})
Vue.component('child-two',{
template:'<div>child-two</div>',
})
var vue = new Vue({
el:"#app",
data:{
type:'child-one',
},
methods:{
change:function () {
this.type = this.type === 'child-one' ? 'child-two' : 'child-one';
}
}
})
</script>
</html>
mode="out-in"先隐藏在显示
mode="in-out"先出现在隐藏
v-bind:is=""实现动态组件