vue中组件v-if、v-else、:is的切换和过渡动画
使用flag标识符结合v-if和v-else切换组件<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content
·
使用flag标识符结合v-if和v-else切换组件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<link href="https://cdn.jsdelivr.net/npm/animate.css@3.5.1" rel="stylesheet" type="text/css">
<title>Document</title>
</head>
<body>
<div id="app">
<input type="button" value="toggle" @click="flag=!flag">
<my-com1 v-if="flag"></my-com1>
<my-com2 v-else="flag"></my-com2>
</div>
<script>
Vue.component('myCom1', {
template: '<h3>我是007</h3>'
})
Vue.component('myCom2', {
template: '<h3>我是009</h3>'
})
var vm = new Vue({
el: '#app',
data: {
flag: true
},
methods: {},
});
</script>
</body>
</html>
效果展示:
使用:is
属性来切换不同的子组件,并添加切换动画
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<link href="https://cdn.jsdelivr.net/npm/animate.css@3.5.1" rel="stylesheet" type="text/css">
<title>Document</title>
</head>
<body>
<div id="app">
<input type="radio" v-model="gender" value="man">
<input type="radio" v-model="gender" value="woman">
<transition mode="out-in">
<component :is="gender"></component>
</transition>
<hr>
<input type="button" value="登录" @click="className='login'">
<input type="button" value="注册" @click="className='register'">
<transition mode="out-in">
<component :is="className"></component>
</transition>
</div>
<script>
Vue.component('man', {
template: '<h3>男</h3>'
})
Vue.component('woman', {
template: '<h3>女</h3>'
})
Vue.component('login', {
template: '<h3>登录</h3>'
})
Vue.component('register', {
template: '<h3>注册</h3>'
})
var vm = new Vue({
el: '#app',
data: {
gender:'man',
className:'login'
},
methods: {},
});
</script>
</body>
</html>
效果展示:
可添加动画切换样式
<style>
.v-enter,
.v-leave-to {
opacity: 0;
transform: translateX(30px);
}
.v-enter-active,
.v-leave-active {
position: relative;
transition: all 0.3s ease;
}
h3{
margin: 0;
}
</style>
更多推荐
已为社区贡献7条内容
所有评论(0)