Vue-组件4 组件切换
2020-01-13 04:52
267 查看
Vue-组件4 组件切换
一、组件切换方式1—component元素
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <script src="../lib/vue.js"></script> <style> .comSty { border: 1px solid black; width: 255px; padding: 10px; } .labelSty { width:80px; display: inline-block; } </style> </head> <body> <div id="app"> <!-- 4 两个超链接,用来切换组件--> <a href="" @click.prevent="showComId='login'">登录</a> <a href="" @click.prevent="showComId='register'">注册</a> <!-- 5 用component元素来显示组件,is属性为显示组件的id--> <component :is="showComId"></component> </div> <!-- 1.定义两个组件模板,登录和注册--> <!-- 1.1 登录组件模板--> <template id="tmp-login"> <div class="comSty"> <label for="username" class="labelSty">用户名:</label><input id="username" type="text"><br> <label for="password" class="labelSty">密码:</label><input id="password" type="password"><br> <button>登录</button> </div> </template> <!-- 1.2 注册组件模板--> <template id="tmp-registered"> <div class="comSty"> <label for="username" class="labelSty">用户名:</label><input id="username" type="text"><br> <label for="password" class="labelSty">密码:</label><input id="password" type="password"><br> <label for="password" class="labelSty">确认密码:</label><input id="password" type="password"><br> <button>注册</button> </div> </template> <script> // 2 创建组件 // 2.1 应用登录组件模板 Vue.component('login', { template: '#tmp-login' }) // 2.2 应用注册组件模板 Vue.component('register', { template: '#tmp-registered' }) var vm = new Vue({ el: '#app', data: { showComId: 'login' // 3 定义当前组件id }, methods: {} }) </script> </body> </html>
二、给组件添加动画
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <script src="../lib/vue.js"></script> <style> .comSty { border: 1px solid black; width: 255px; padding: 10px; } .labelSty { width:80px; display: inline-block; } .v-enter, .v-leave-to { opacity: 0; transform: translateX(100px); } .v-enter-active, .v-leave-avtive { transition: all 0.8s ease; } </style> </head> <body> <div id="app"> <!-- 4 两个超链接,用来切换组件--> <a href="" @click.prevent="showComId='login'">登录</a> <a href="" @click.prevent="showComId='register'">注册</a> <!-- 5 用component元素来显示组件,is属性为显示组件的id--> <!-- 使用transition来设置动画,mode="out-in"为先退出再进入--> <transition mode="out-in"> <component :is="showComId"></component> </transition> </div> <!-- 1.定义两个组件模板,登录和注册--> <!-- 1.1 登录组件模板--> <template id="tmp-login"> <div class="comSty"> <label for="username" class="labelSty">用户名:</label><input id="username" type="text"><br> <label for="password" class="labelSty">密码:</label><input id="password" type="password"><br> <button>登录</button> </div> </template> <!-- 1.2 注册组件模板--> <template id="tmp-registered"> <div class="comSty"> <label for="username" class="labelSty">用户名:</label><input id="username" type="text"><br> <label for="password" class="labelSty">密码:</label><input id="password" type="password"><br> <label for="password" class="labelSty">确认密码:</label><input id="password" type="password"><br> <button>注册</button> </div> </template> <script> // 2 创建组件 // 2.1 应用登录组件模板 Vue.component('login', { template: '#tmp-login' }) // 2.2 应用注册组件模板 Vue.component('register', { template: '#tmp-registered' }) var vm = new Vue({ el: '#app', data: { showComId: 'login' // 3 定义当前组件id }, methods: {} }) </script> </body> </html>更新时间:2019-12-21
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- Vue.js组件tab实现选项卡切换
- 详解vue2.0 使用动态组件实现 Tab 标签页切换效果(vue-cli)
- Vue组件切换--带动画效果
- 28vue学习——component 元素实现两个以上的组件切换
- vue笔记之组件切换
- vue动态组件实现选项卡切换效果
- Vue-使用i18n实现中英文切换功能,表单验证,组件数据等
- elementui中tab切换组件引入vue组件不显示的问题
- Vue记录 this.$emit 子组件向父组件传递事件(导航的切换)
- 基于Vue开发的tab切换组件
- VUE-解决切换页面后,子组件通过props获取的父组件数据不同步的问题
- vue学习笔记:6.2.组件的切换方式
- vue-lazy-render: 延迟渲染大组件,增强页面切换流畅度
- Vue教程(组件-切换案例)
- Vue2.0 多 Tab切换组件的封装实例
- 浅谈vue-router路由切换 组件重用挖下的坑
- vue-router组件重用 路由切换时的问题
- vue2.0使用动态组件实现tab切换效果(vue-cli)
- 解决ios微信下vue项目组件切换并自动播放音频问题
- 详解vue2.0 使用动态组件实现 Tab 标签页切换效果(vue-cli)