Vue动态组件实例解析

 更新时间:2017年08月20日 11:20:12   作者:小火柴的蓝色理想  
让多个组件使用同一个挂载点,并动态切换,这就是动态组件。这篇文章主要介绍了Vue动态组件 ,需要的朋友可以参考下

前面的话

  让多个组件使用同一个挂载点,并动态切换,这就是动态组件。本文将详细介绍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动态组件实例解析,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

相关文章

  • Vue websocket封装实现方法详解

    Vue websocket封装实现方法详解

    项目中需要使用websocke,这个是我自己修修改改好多次之后用得最顺手的一版,分享一下。这个封装需要搭配vuex使用,因为收到的数据都保存在vuex中了,真的绝绝子好用,解决了我一大堆问题
    2022-09-09
  • 详解Vue.js动态绑定class

    详解Vue.js动态绑定class

    Vue.js的核心是一个响应的数据绑定系统,它允许我们在普通 HTML 模板中使用特殊的语法将 DOM “绑定”到底层数据。被绑定的DOM 将与数据保持同步,每当数据有改动,相应的DOM视图也会更新。基于这种特性,通过vue.js动态绑定class就变得非常简单。一起来看下吧
    2016-12-12
  • vue-router的导航守卫使用最新讲解

    vue-router的导航守卫使用最新讲解

    这篇文章主要介绍了vue-router的导航守卫使用讲解,vue-router提供了许多编程式导航的API,其中最常见的导航API有很多种,本文给大家详细讲解,需要的朋友可以参考下
    2022-12-12
  • Vue路由传递query参数两种方式

    Vue路由传递query参数两种方式

    路由是可以传递参数的,一般使用query进行传参,有两种方式,本温酒通过代码示例给大家介绍这两种传递方式,感兴趣的小伙伴可以参考阅读
    2023-06-06
  • vue.js移动端tab组件的封装实践实例

    vue.js移动端tab组件的封装实践实例

    本篇文章主要介绍了vue.js移动端tab的封装实践实例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • 详解vue中$router和$route的区别

    详解vue中$router和$route的区别

    这篇文章主要介绍了vue中$router和$route的区别,对vue感兴趣的同学,一定要看下
    2021-05-05
  • 解决element-ui el-checkbox的一些坑

    解决element-ui el-checkbox的一些坑

    这篇文章主要介绍了解决element-ui el-checkbox的一些坑,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • webstorm和.vue中es6语法报错的解决方法

    webstorm和.vue中es6语法报错的解决方法

    本篇文章主要介绍了webstorm和.vue中es6语法报错的解决方法,小编总结了webstorm和.vue中出现的es6语法报错,避免大家采坑,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • vue实现分页栏效果

    vue实现分页栏效果

    这篇文章主要为大家详细介绍了vue实现分页栏效果,分页栏设计的步骤与实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-06-06
  • 浅谈Vuejs Prop基本用法

    浅谈Vuejs Prop基本用法

    本篇文章主要介绍了Vuejs Prop基本用法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08

最新评论