20 vue之动态组件+vue之keep-alive
2022/2/14 23:44:19
本文主要是介绍20 vue之动态组件+vue之keep-alive,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1 动态组件
1 <component> 元素,动态地绑定多个组件到它的 is 属性
2 <keep-alive> 保留状态,避免重新渲染
2 基本使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>ref放在子组件上</title> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.12/vue.min.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> </head> <body> <div id="box"> <span @click="who='child1'">首页</span> <span @click="who='child2'">商品</span> <span @click="who='child3'">购物</span> <hr> <componet :is="who"></componet> </div> </body> <script> Vue.component('child1', { template: `<div> 首页 </div>`, }) Vue.component('child2', { template: `<div> 商品 </div>`, }) Vue.component('child3', { template: `<div> 购物车 </div>`, }) let vm = new Vue({ el: '#box', data: { who:'child1' }, methods:{ } }) </script> </html>
二、vue之keep-alive
1 不使用keep-alive
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>动态组件</title> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.12/vue.min.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> </head> <body> <div id="box"> <span @click="who='child1'">首页</span> <span @click="who='child2'">商品</span> <span @click="who='child3'">购物</span> <hr> <componet :is="who"></componet> </div> </body> <script> Vue.component('child1', { template: `<div> 首页 </div>`, }) Vue.component('child2', { template: `<div> 商品 <input type="text"> </div>`, }) Vue.component('child3', { template: `<div> 购物车 </div>`, }) let vm = new Vue({ el: '#box', data: { who:'child1' }, methods:{ } }) </script> </html>
2 使用keep-alive
keep-alive 保留状态,避免重新渲染
<keep-alive> <component :is="who"></component> </keep-alive>
案例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>动态组件</title> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.12/vue.min.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> </head> <body> <div id="box"> <span @click="who='child1'">首页</span> <span @click="who='child2'">商品</span> <span @click="who='child3'">购物</span> <hr> <keep-alive> <componet :is="who"></componet> </keep-alive> </div> </body> <script> Vue.component('child1', { template: `<div> 首页 </div>`, }) Vue.component('child2', { template: `<div> 商品 <input type="text"> </div>`, }) Vue.component('child3', { template: `<div> 购物车 </div>`, }) let vm = new Vue({ el: '#box', data: { who:'child1' }, methods:{ } }) </script> </html>
这篇关于20 vue之动态组件+vue之keep-alive的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-21Vue3教程:新手入门到实践应用
- 2024-12-21VueRouter4教程:从入门到实践
- 2024-12-20Vue3项目实战:从入门到上手
- 2024-12-20Vue3项目实战:新手入门教程
- 2024-12-20VueRouter4项目实战:新手入门教程
- 2024-12-20如何实现JDBC和jsp的关系?-icode9专业技术文章分享
- 2024-12-20Vue项目中实现TagsView标签栏导航的简单教程
- 2024-12-20Vue3入门教程:从零开始搭建你的第一个Vue3项目
- 2024-12-20从零开始学习vueRouter4:基础教程
- 2024-12-20Vuex4课程:新手入门到上手实战全攻略