Vue动态组件实例解析


Posted in Javascript onAugust 20, 2017

前面的话

让多个组件使用同一个挂载点,并动态切换,这就是动态组件。本文将详细介绍Vue动态组件

概述

通过使用保留的 <component> 元素,动态地绑定到它的 is 特性,可以实现动态组件

<div id="example">
 <button @click="change">切换页面</button>
 <component :is="currentView"></component>
</div>
<script>
var home = {template:'<div>我是主页</div>'};
var post = {template:'<div>我是提交页</div>'};
var archive = {template:'<div>我是存档页</div>'};
new Vue({
 el: '#example',
 components: {
  home,
  post,
  archive,
 },
 data:{
  index:0,
  arr:['home','post','archive'],
 },
 computed:{
  currentView(){
    return this.arr[this.index];
  }
 },
 methods:{
  change(){
   this.index = (++this.index)%3;
  }
 }
})
</script>

也可以直接绑定到组件对象上

<div id="example">
 <button @click="change">切换页面</button>
 <component :is="currentView"></component>
</div>
<script>
new Vue({
 el: '#example',
 data:{
  index:0,
  arr:[
   {template:`<div>我是主页</div>`},
   {template:`<div>我是提交页</div>`},
   {template:`<div>我是存档页</div>`}
  ],
 },
 computed:{
  currentView(){
    return this.arr[this.index];
  }
 },
 methods:{
  change(){
   this.index = (++this.index)%3;
  }
 }
})
</script>

缓存 

<keep-alive> 包裹动态组件时,会缓存不活动的组件实例,而不是销毁它们。和 <transition> 相似,<keep-alive> 是一个抽象组件:它自身不会渲染一个 DOM 元素,也不会出现在父组件链中

【基础用法】

<div id="example">
 <button @click="change">切换页面</button>
 <keep-alive>
  <component :is="currentView"></component> 
 </keep-alive>
</div>

<script>
new Vue({
 el: '#example',
 data:{
  index:0,
  arr:[
   {template:`<div>我是主页</div>`},
   {template:`<div>我是提交页</div>`},
   {template:`<div>我是存档页</div>`}
  ],
 },
 computed:{
  currentView(){
    return this.arr[this.index];
  }
 },
 methods:{
  change(){
   let len = this.arr.length;
   this.index = (++this.index)% len;
  }
 }
})
</script>

【条件判断】

如果有多个条件性的子元素,<keep-alive> 要求同时只有一个子元素被渲染

<div id="example">
 <button @click="change">切换页面</button>
 <keep-alive>
  <home v-if="index===0"></home>
  <posts v-else-if="index===1"></posts>
  <archive v-else></archive> 
 </keep-alive>
</div>

<script>
new Vue({
 el: '#example',
 components:{
  home:{template:`<div>我是主页</div>`},
  posts:{template:`<div>我是提交页</div>`},
  archive:{template:`<div>我是存档页</div>`},
 },
 data:{
  index:0,
 },
 methods:{
  change(){
   let len = Object.keys(this.$options.components).length;
   this.index = (++this.index)%len;
  }
 }
})
</script>

【activated 和 deactivated】

activated 和 deactivated 在 <keep-alive> 树内的所有嵌套组件中触发

<div id="example">
 <button @click="change">切换页面</button>
 <keep-alive>
  <component :is="currentView" @pass-data="getData"></component> 
 </keep-alive>
 <p>{{msg}}</p>
</div>

<script>
new Vue({
 el: '#example',
 data:{
  index:0,
  msg:'',  
  arr:[
   { 
    template:`<div>我是主页</div>`,
    activated(){
     this.$emit('pass-data','主页被添加');
    },
    deactivated(){
     this.$emit('pass-data','主页被移除');
    },    
   },
   {template:`<div>我是提交页</div>`},
   {template:`<div>我是存档页</div>`}
  ],
 },
 computed:{
  currentView(){
    return this.arr[this.index];
  }
 },
 methods:{
  change(){
   var len = this.arr.length;
   this.index = (++this.index)% len;
  },
  getData(value){
   this.msg = value;
   setTimeout(()=>{
    this.msg = '';
   },500)
  }
 }
})
</script>

【include和exclude】

include 和 exclude 属性允许组件有条件地缓存。二者都可以用逗号分隔字符串、正则表达式或一个数组来表示

<!-- 逗号分隔字符串 -->
<keep-alive include="a,b">
 <component :is="view"></component>
</keep-alive>
<!-- 正则表达式 (使用 v-bind) -->
<keep-alive :include="/a|b/">
 <component :is="view"></component>
</keep-alive>
<!-- Array (use v-bind) -->
<keep-alive :include="['a', 'b']">
 <component :is="view"></component>
</keep-alive>

匹配首先检查组件自身的 name 选项,如果 name 选项不可用,则匹配它的局部注册名称(父组件 components 选项的键值)。匿名组件不能被匹配

<keep-alive include="home,archive">
  <component :is="currentView"></component> 
 </keep-alive>

上面的代码,表示只缓存home和archive,不缓存posts

<div id="example">
 <button @click="change">切换页面</button>
 <keep-alive include="home,archive">
  <component :is="currentView"></component> 
 </keep-alive>
</div>
<script src="https://unpkg.com/vue"></script>
<script>
new Vue({
 el: '#example',
 data:{
  index:0,  
  arr:[
   {name:'home',template:`<div>我是主页</div>`},
   {name:'posts',template:`<div>我是提交页</div>`},
   {name:'archive',template:`<div>我是存档页</div>`}
  ],
 },
 computed:{
  currentView(){
    return this.arr[this.index];
  }
 },
 methods:{
  change(){
   var len = this.arr.length;
   this.index = (++this.index)% len;
  },
 }
})
</script>

总结

以上所述是小编给大家介绍的Vue动态组件实例解析,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

Javascript 相关文章推荐
jQuery中slideUp()方法用法分析
Dec 24 Javascript
使用Browserify配合jQuery进行编程的超级指南
Jul 28 Javascript
js实现简单排列组合的方法
Jan 27 Javascript
浅谈JS原型对象和原型链
Mar 02 Javascript
jQuery each函数源码分析
May 25 Javascript
VsCode插件整理(小结)
Sep 14 Javascript
简单谈谈vue的过渡动画(推荐)
Oct 11 Javascript
vue中rem的配置的方法示例
Aug 30 Javascript
微信JS-SDK实现微信会员卡功能(给用户微信卡包里发送会员卡)
Jul 25 Javascript
Vue两种组件类型:递归组件和动态组件的用法
Aug 06 Javascript
Nest.js散列与加密实例详解
Feb 24 Javascript
一文搞懂redux在react中的初步用法
Jun 09 Javascript
jQuery实现表格冻结顶栏效果
Aug 20 #jQuery
Vue组件实例间的直接访问实现代码
Aug 20 #Javascript
JavaScript贪吃蛇小组件实例代码
Aug 20 #Javascript
React Native 环境搭建的教程
Aug 19 #Javascript
ionic App问题总结系列之ionic点击系统返回键退出App
Aug 19 #Javascript
浅谈关于.vue文件中style的scoped属性
Aug 19 #Javascript
如何理解Vue的作用域插槽的实现原理
Aug 19 #Javascript
You might like
星际RPG字典
2020/03/04 星际争霸
Session的工作方式
2006/10/09 PHP
php 图像函数大举例(非原创)
2009/06/20 PHP
PHP gbk环境下json_dencode传送来的汉字
2012/11/13 PHP
使用php的HTTP请求的库Requests实现美女图片墙
2015/02/22 PHP
Zend Framework教程之模型Model用法简单实例
2016/03/04 PHP
Aster vs Newbee BO5 第三场2.19
2021/03/10 DOTA
Jsonp 跨域的原理以及Jquery的解决方案
2010/05/18 Javascript
cnblogs中在闪存中屏蔽某人的实现代码
2010/11/14 Javascript
jQuery遍历Table应用示例
2014/04/09 Javascript
jQery使网页在显示器上居中显示适用于任何分辨率
2014/06/09 Javascript
基于socket.io和node.js搭建即时通信系统
2014/07/30 Javascript
jQuery实现个性翻牌效果导航菜单的方法
2015/03/09 Javascript
javascript搜索框效果实现方法
2015/05/14 Javascript
实现音乐播放器的代码(html5+css3+jquery)
2015/08/04 Javascript
js+css3制作时钟特效
2016/10/16 Javascript
jqGrid翻页时数据选中丢失问题的解决办法
2017/02/13 Javascript
jQuery鼠标移动图片上实现放大效果
2017/06/25 jQuery
Vuex mutitons和actions初使用详解
2019/03/04 Javascript
vue实现网络图片瀑布流 + 下拉刷新 + 上拉加载更多(步骤详解)
2020/01/14 Javascript
antd vue 刷新保留当前页面路由,保留选中菜单,保留menu选中操作
2020/08/06 Javascript
vue全局使用axios的操作
2020/09/08 Javascript
python让图片按照exif信息里的创建时间进行排序的方法
2015/03/16 Python
查看Django和flask版本的方法
2018/05/14 Python
Python生成短uuid的方法实例详解
2018/05/29 Python
python实现微信自动回复机器人功能
2019/07/11 Python
TFRecord格式存储数据与队列读取实例
2020/01/21 Python
Python datetime模块使用方法小结
2020/06/18 Python
美国保健品专家:Life Extension
2018/05/04 全球购物
英国独特家具和家庭用品购物网站:Cuckooland
2020/08/30 全球购物
SIMON MILLER官网:洛杉矶的生活方式品牌
2020/10/19 全球购物
事务机电主管工作职责
2014/02/25 职场文书
初婚未育证明样本
2014/10/24 职场文书
报名委托书
2015/01/29 职场文书
小学科学课教学反思
2016/02/23 职场文书
MySQL实战记录之如何快速定位慢SQL
2022/03/23 MySQL