Vuejs 動態組件


http://www.cnblogs.com/-ding/p/6339740.html

①簡單來說:

就是幾個組件放在一個掛載點下,然后根據父組件的某個變量來決定顯示哪個,或者都不顯示。

 

②動態切換:

在掛載點使用component標簽,然后使用v-bind:is=”組件名”,會自動去找匹配的組件名,如果沒有,則不顯示;

 

改變掛載的組件,只需要修改is指令的值即可。

 

如示例代碼:

[html]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. <div id="app">  
  2.     <button @click="toshow">點擊讓子組件顯示</button>  
  3.     <component v-bind:is="which_to_show"></component>  
  4. </div>  
  5. <script>  
  6.     var vm = new Vue({  
  7.         el: '#app',  
  8.         data: {  
  9.             which_to_show: "first"  
  10.         },  
  11.         methods: {  
  12.             toshow: function () {   //切換組件顯示  
  13.                 var arr = ["first", "second", "third", ""];  
  14.                 var index = arr.indexOf(this.which_to_show);  
  15.                 if (index 3) {  
  16.                     this.which_to_show = arr[index + 1];  
  17.                 } else {  
  18.                     this.which_to_show = arr[0];  
  19.                 }  
  20.             }  
  21.         },  
  22.         components: {  
  23.             first: { //第一個子組件  
  24.                 template: "<div>這里是子組件1</div>"  
  25.             },  
  26.             second: { //第二個子組件  
  27.                 template: "<div>這里是子組件2</div>"  
  28.             },  
  29.             third: { //第三個子組件  
  30.                 template: "<div>這里是子組件3</div>"  
  31.             },  
  32.         }  
  33.     });  
  34. </script>  

說明:

點擊父組件的按鈕,會自動切換顯示某一個子組件(根據which_to_show這個變量的值來決定)。

 

 

 

③keep-alive

簡單來說,被切換掉(非當前顯示)的組件,是直接被移除了。

在父組件中查看this.$children屬性,可以發現,當子組件存在時,該屬性的length為1,而子組件不存在時,該屬性的length是0(無法獲取到子組件);

 

假如需要子組件在切換后,依然需要他保留在內存中,避免下次出現的時候重新渲染。那么就應該在component標簽中添加keep-alive屬性。

 

如代碼:

[html]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. <div id="app">  
  2.     <button @click="toshow">點擊讓子組件顯示</button>  
  3.     <component v-bind:is="which_to_show" keep-alive></component>  
  4. </div>  
  5. <script>  
  6.     var vm = new Vue({  
  7.         el: '#app',  
  8.         data: {  
  9.             which_to_show: "first"  
  10.         },  
  11.         methods: {  
  12.             toshow: function () {   //切換組件顯示  
  13.                 var arr = ["first", "second", "third", ""];  
  14.                 var index = arr.indexOf(this.which_to_show);  
  15.                 if (index 3) {  
  16.                     this.which_to_show = arr[index + 1];  
  17.                 } else {  
  18.                     this.which_to_show = arr[0];  
  19.                 }  
  20.                 console.log(this.$children);  
  21.             }  
  22.         },  
  23.         components: {  
  24.             first: { //第一個子組件  
  25.                 template: "<div>這里是子組件1</div>"  
  26.             },  
  27.             second: { //第二個子組件  
  28.                 template: "<div>這里是子組件2</div>"  
  29.             },  
  30.             third: { //第三個子組件  
  31.                 template: "<div>這里是子組件3</div>"  
  32.             },  
  33.         }  
  34.     });  
  35. </script>  

 

說明:

初始情況下,vm.$children屬性中只有一個元素(first組件),

點擊按鈕切換后,vm.$children屬性中有兩個元素,

再次切換后,則有三個元素(三個子組件都保留在內存中)。

之后無論如何切換,將一直保持有三個元素。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM