vue動態子組件的實現方式

讓多個組件使用同一個掛載點,並動態切換,這就是動態組件。html

經過使用保留的 <component>元素,動態地綁定到它的 is 特性,能夠實現動態組件。vue

方式一:局部註冊所需組件

<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>

使用<keep-alive>緩存

<keep-alive> 包裹動態組件時,會緩存不活動的組件實例,而不是銷燬它們。和 <transition>類似,<keep-alive> 是一個抽象組件:它自身不會渲染一個 DOM 元素,也不會出如今父組件鏈中。正則表達式

基本用法:segmentfault

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

條件判斷數組

若是有多個條件性的子元素,<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 和 deactivatedasync

activated 和 deactivated 在 <keep-alive> 樹內的全部嵌套組件中觸發:post

<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和excludeui

include 和exclude屬性容許組件有條件地緩存。兩者均可以用逗號分隔字符串、正則表達式或一個數組來表示:this

<!-- 逗號分隔字符串 -->
<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

方式二:動態註冊組件實現

 asyncComponents(templateName){
    this.curNavComponents = require(`./components/${templateName}.vue`).default;
}

參考地址:

相關文章
相關標籤/搜索