国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Vue動態組件實例解析

2019-11-19 15:44:06
字體:
來源:轉載
供稿:網友

前面的話

  讓多個組件使用同一個掛載點,并動態切換,這就是動態組件。本文將詳細介紹Vue動態組件

概述

  通過使用保留的 <component> 元素,動態地綁定到它的 is 特性,可以實現動態組件

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

  也可以直接綁定到組件對象上

<div id="example"> <button @click="change">切換頁面</button> <component :is="currentView"></component></div><script>new Vue({ el: '#example', data:{  index:0,  arr:[   {template:`<div>我是主頁</div>`},   {template:`<div>我是提交頁</div>`},   {template:`<div>我是存檔頁</div>`}  ], }, computed:{  currentView(){    return this.arr[this.index];  } }, methods:{  change(){   this.index = (++this.index)%3;  } }})</script>

緩存 

  <keep-alive> 包裹動態組件時,會緩存不活動的組件實例,而不是銷毀它們。和 <transition> 相似,<keep-alive> 是一個抽象組件:它自身不會渲染一個 DOM 元素,也不會出現在父組件鏈中

【基礎用法】

<div id="example"> <button @click="change">切換頁面</button> <keep-alive>  <component :is="currentView"></component>  </keep-alive></div><script>new Vue({ el: '#example', 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="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 和 deactivated】

  activated 和 deactivated 在 <keep-alive> 樹內的所有嵌套組件中觸發

<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和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="example"> <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: '#example', 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動態組件實例解析,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泰兴市| 景德镇市| 巴彦淖尔市| 宜兰县| 贵港市| 达拉特旗| 海城市| 胶南市| 隆子县| 高邮市| 文山县| 柞水县| 辽宁省| 都匀市| 隆昌县| 丹巴县| 八宿县| 太原市| 宁强县| 谷城县| 东宁县| 苗栗县| 兴和县| 宁明县| 阿鲁科尔沁旗| 方山县| 鲁山县| 利川市| 桐庐县| 云浮市| 丹巴县| 新蔡县| 收藏| 库尔勒市| 松溪县| 湄潭县| 武邑县| 赤城县| 漳浦县| 澄江县| 汽车|