内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

vue動態(tài)組件用法示例小結

本文實例講述了vue 動態(tài)組件用法。分享給大家供大家參考,具體如下:

發(fā)展壯大離不開廣大客戶長期以來的信賴與支持,我們將始終秉承“誠信為本、服務至上”的服務理念,堅持“二合一”的優(yōu)良服務模式,真誠服務每家企業(yè),認真做好每個細節(jié),不斷完善自我,成就企業(yè),實現(xiàn)共贏。行業(yè)涉及衛(wèi)生間隔斷等,在成都網站建設全網營銷推廣、WAP手機網站、VI設計、軟件開發(fā)等項目上具有豐富的設計經驗。

通過使用保留的 <component> 元素,動態(tài)地綁定到它的 is 特性,我們讓多個組件可以使用同一個掛載點,并動態(tài)切換。根據(jù) v-bind:is="組件名" 中的組件名去自動匹配組件,如果匹配不到則不顯示。

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

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 測試實例 - 動態(tài)組件</title>
<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
 <button @click='toShow'>點擊顯示子組件</button>
 <component v-bind:is="which_to_show"></component>
</div>

<script>

// 創(chuàng)建根實例
new Vue({
 el: '#app',
 data:{
  which_to_show:'first'
 },
 methods:{
 toShow:function(){
  var arr = ["first","second","third"];
  var index = arr.indexOf(this.which_to_show);
  if(index<2){
  this.which_to_show = arr[index+1];
  }else{
  this.which_to_show = arr[0];
  }
 }
 },
 components:{
 first:{
  template:'<div>這是子組件1<div>'
 },
 second:{
  template:'<div>這是子組件2<div>'
 },
 third:{
  template:'<div>這是子組件3<div>'
 },
 }
})
</script>
</body>
</html>

vue 動態(tài)組件用法示例小結

#keep-alive

動態(tài)切換掉的組件(非當前顯示的組件)是被移除掉了,如果把切換出去的組件保留在內存中,可以保留它的狀態(tài)或避免重新渲染。為此可以添加一個 keep-alive 指令參數(shù):

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 測試實例 - 動態(tài)組件</title>
<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
 <button @click='toShow'>點擊顯示子組件</button>
 <!----或者<component v-bind:is="which_to_show" keep-alive></component>也行----->
 <keep-alive>
 <component v-bind:is="which_to_show" ></component>
 </keep-alive>
</div>

<script>

// 創(chuàng)建根實例
new Vue({
 el: '#app',
 data:{
  which_to_show:'first'
 },
 methods:{
 toShow:function(){
  var arr = ["first","second","third"];
  var index = arr.indexOf(this.which_to_show);
  if(index<2){
  this.which_to_show = arr[index+1];
  }else{
  this.which_to_show = arr[0];
  } console.log(this.$children); 
 }
 },
 components:{
 first:{
  template:'<div>這是子組件1<div>'
 },
 second:{
  template:'<div>這是子組件2<div>'
 },
 third:{
  template:'<div>這是子組件3<div>'
 },
 }
})
</script>
</body>
</html>

說明:

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

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

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

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

actived鉤子

可以延遲執(zhí)行當前的組件。

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

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 測試實例 - 動態(tài)組件</title>
<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
 <button @click='toShow'>點擊顯示子組件</button>
 <!----或者<component v-bind:is="which_to_show" keep-alive></component>也行----->
 <keep-alive>
 <component v-bind:is="which_to_show" ></component>
 </keep-alive>
</div>

<script>

// 創(chuàng)建根實例
var vm = new Vue({
    el: '#app',
    data: {
      which_to_show: "first"
    },
    methods: {
      toShow: function () {  //切換組件顯示
        var arr = ["first", "second", "third", ""];
        var index = arr.indexOf(this.which_to_show);
        if (index < 2) {
          this.which_to_show = arr[index + 1];
        } else {
          this.which_to_show = arr[0];
        }
        console.log(this.$children);
      }
    },
    components: {
      first: { //第一個子組件
        template: "<div>這里是子組件1</div>"
      },
      second: { //第二個子組件
        template: "<div>這里是子組件2,這里是延遲后的內容:{{hello}}</div>",
        data: function () {
          return {
            hello: ""
          }
        },
        activated: function (done) { //執(zhí)行這個參數(shù)時,才會切換組件
   console.log('hhh')
          var self = this;
   var startTime = new Date().getTime(); // get the current time
   //兩秒后執(zhí)行
          while (new Date().getTime() < startTime + 2000){
   self.hello='我是延遲后的內容';
   }

        }
      },
      third: { //第三個子組件
        template: "<div>這里是子組件3</div>"
      }
    }
  });
</script>
</body>
</html>

vue 動態(tài)組件用法示例小結

當切換到第二個組件的時候,會先執(zhí)行activated鉤子,會在兩秒后顯示組件2.起到了延遲加載的作用。

希望本文所述對大家vue.js程序設計有所幫助。

當前標題:vue動態(tài)組件用法示例小結
當前網址:http://www.rwnh.cn/article42/jeshec.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供網站導航、網站建設標簽優(yōu)化、網站制作、網站設計、企業(yè)網站制作

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

網站托管運營
宜章县| 涟源市| 永安市| 广南县| 泰和县| 含山县| 东源县| 蓬莱市| 亳州市| 河北区| 小金县| 宝山区| 天长市| 冀州市| 阳曲县| 永兴县| 孟连| 临武县| 诸暨市| 阿克陶县| 北安市| 高雄市| 安顺市| 襄樊市| 中西区| 沐川县| 西安市| 商水县| 阳山县| 高要市| 公主岭市| 商都县| 兰州市| 固始县| 开阳县| 苍南县| 安阳县| 乌兰察布市| 大丰市| 鞍山市| 平和县|