<!DOCTYPE html>
<html>
<head>
<title>vue多个元素或多组件的过度</title>
<script src="./vue.js"></script>
<style>
.v-enter,
.v-leave-to {
opacity: 0;
}
.v-enter-active,
.v-leave-active {
transition: opacity 2s;
}
</style>
</head>
<body>
<div id='app'>
<!-- // 多元素过度 -->
<transition
mode="out-in"
>
<div v-if="show" key="hello">hello world</div>
<div v-else key="bye">Bye</div>
</transition>
<button @click='handleClick'>切换</button>
</div>
<!-- // 多组件过度 -->
<div id='app-1'>
<transition
mode="out-in"
>
<component :is="type"></component>
<!-- // 原生组件 -->
<!-- <child v-if="show"></child>
<child-one v-else></child-one> -->
</transition>
<button @click='handleClick'>切换</button>
</div>
<script>
var vm1 = new Vue({
el: '#app',
data: {
show: true
},
methods: {
handleClick: function() {
this.show = !this.show
}
}
})
Vue.component('child',{
template: '<div>child</div>'
})
Vue.component('child-one',{
template: '<div>child-one</div>'
})
var vm2 = new Vue({
el: '#app-1',
data: {
// show: true
type: 'child'
},
methods: {
handleClick: function() {
// this.show = !this.show
this.type = this.type === 'child' ? 'child-one' : 'child'
}
}
})
</script>
</body>
</html>
网友评论