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

首頁 > 語言 > JavaScript > 正文

Vue動態(tài)組件實例解析

2024-05-06 15:12:36
字體:
供稿:網(wǎng)友

前面的話

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

概述

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

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

【基礎(chǔ)用法】

<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>            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 绵阳市| 禹城市| 桐庐县| 桃园市| 镇沅| 磐石市| 红河县| 杭锦旗| 体育| 睢宁县| 祁东县| 米林县| 班玛县| 乐东| 东丽区| 保靖县| 潜山县| 凌源市| 辽宁省| 慈利县| 庆云县| 浦县| 安新县| 汝州市| 九龙城区| 大理市| 元谋县| 平度市| 西和县| 健康| 阿克陶县| 神池县| 克山县| 古交市| 板桥市| 延庆县| 盐山县| 阜宁县| 武乡县| 湖州市| 青州市|