Vue动态组件

  让多个组件使用同一个挂载点,并动态切换,这就是动态组件。

概述

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

<div id="app">
  <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: '#app',
  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>

缓存

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

<div id="app">
  <button @click="change">切换页面</button>
  <keep-alive>
    <component :is="currentView"></component>  
  </keep-alive>
</div>
<script>
new Vue({
  el: '#app',
  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="app">
  <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: '#app',
  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="app">
  <button @click="change">切换页面</button>
  <keep-alive>
    <component :is="currentView" @pass-data="getData"></component> 
  </keep-alive>
  <p>{{msg}}</p>
</div>
<script>
new Vue({
  el: '#app',
  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="app">
  <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: '#app',
  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动态组件上的坑上重复踩了两次,一模一样的两次思考过程。 我想,这个重复思考的过程并不是偶然,说明是很有必...
    嘉宝_Appian阅读 25,016评论 6 19
  • Vue 动态组件 通过 Vue 的 <component> 元素加一个特殊的 is 特性来实现。currentCo...
    ChangLau阅读 2,200评论 0 2
  • 在动态组件中使用 keep-alive 在动态组件中使用keep-alive可一缓存组件 在组件切换的时候不会重新...
    尼莫nemo阅读 1,512评论 0 0
  • vue与jquery相比,最大的变化就是大大减少了节点的操作,组件化更是好用到不行。往日都是用jquery做tab...
    Cherry9507阅读 7,288评论 4 3
  • 本文写给对Vue组件了解的基础上,如果你不明白什么是Vue的组件,可以去官网看看Vue组件基础。在Vue中有一个 ...
    刘圣凯阅读 12,680评论 0 6