Vuejs——(12)組件——動態組件


 

目錄(?)[+]

 

 

本篇資料來於官方文檔:

http://cn.vuejs.org/guide/components.html#u52A8_u6001_u7EC4_u4EF6

本文是在官方文檔的基礎上,更加細致的說明,代碼更多更全。

簡單來說,更適合新手閱讀

 


 

(二十九)組件——動態組件

①簡單來說:

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

 

②動態切換:

在掛載點使用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屬性中有兩個元素,

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

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

 

 

 

④activate鈎子

簡單來說,他是延遲加載。

例如,在發起ajax請求時,會需要等待一些時間,假如我們需要在ajax請求完成后,再進行加載,那么就需要用到activate鈎子了。

 

具體用法來說,activate是和template、data等屬性平級的一個屬性,形式是一個函數,函數里默認有一個參數,而這個參數是一個函數,執行這個函數時,才會切換組件。

 

為了證明他的延遲加載性,在服務器端我設置當發起某個ajax請求時,會延遲2秒才返回內容,因此,第一次切換組件2時,需要等待2秒才會成功切換:

[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.                 console.log(this.$children);  
  21.             }  
  22.         },  
  23.         components: {  
  24.             first: { //第一個子組件  
  25.                 template: "<div>這里是子組件1</div>"  
  26.             },  
  27.             second: { //第二個子組件  
  28.                 template: "<div>這里是子組件2,這里是ajax后的內容:{{hello}}</div>",  
  29.                 data: function () {  
  30.                     return {  
  31.                         hello: ""  
  32.                     }  
  33.                 },  
  34.                 activate: function (done) { //執行這個參數時,才會切換組件  
  35.                     var self = this;  
  36.                     $.get("/test", function (data) {    //這個ajax我手動在服務器端設置延遲為2000ms,因此需要等待2秒后才會切換  
  37.                         self.hello = data;  
  38.                         done(); //ajax執行成功,切換組件  
  39.                     })  
  40.                 }  
  41.             },  
  42.             third: { //第三個子組件  
  43.                 template: "<div>這里是子組件3</div>"  
  44.             }  
  45.         }  
  46.     });  
  47. </script>  

代碼效果:

【1】第一次切換到組件2時,需要等待2秒后才能顯示(因為發起ajax);

 

【2】在有keep-alive的情況下,第二次或之后切換到組件2時,無需等待;但ajax內容,需要在第一次發起ajax兩秒后才會顯示;

 

【3】在無keep-alive的情況下(切換掉后沒有保存在內存中),第二次切換到組件2時,依然需要等待。

 

【4】等待時,不影響再次切換(即等待組件2的時候,再次點擊切換,可以直接切換到組件3);

 

說明:

【1】只有在第一次渲染組件時,才會執行activate,且該函數只會執行一次(在第一次組件出現的時候延遲組件出現)

【2】沒有keep-alive時,每次切換組件出現都是重新渲染(因為之前隱藏時執行了destroy過程),因此會執行activate方法。

 

 

 

⑤transition-mode過渡模式

簡單來說,動態組件切換時,讓其出現動畫效果。(還記不記得在過渡那一節的說明,過渡適用於動態組件)

默認是進入和退出一起完成;(可能造成進入的內容出現在退出內容的下方,這個下方指y軸方面偏下的,等退出完畢后,進入的才會出現在正確的位置);

transition-mode=”out-in”時,動畫是先出后進;

transition-mode=”in-out”時,動畫是先進后出(同默認情況容易出現的問題);

 

示例代碼:(使用自定義過渡名和animate.css文件)

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


免責聲明!

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



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