Vue動(dòng)態(tài)組件實(shí)例解析
前面的話
讓多個(gè)組件使用同一個(gè)掛載點(diǎn),并動(dòng)態(tài)切換,這就是動(dòng)態(tài)組件。本文將詳細(xì)介紹Vue動(dòng)態(tài)組件
概述
通過使用保留的 <component>
元素,動(dòng)態(tài)地綁定到它的 is 特性,可以實(shí)現(xiàn)動(dòng)態(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>
也可以直接綁定到組件對(duì)象上
<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> 包裹動(dòng)態(tài)組件時(shí),會(huì)緩存不活動(dòng)的組件實(shí)例,而不是銷毀它們。和 <transition> 相似,<keep-alive> 是一個(gè)抽象組件:它自身不會(huì)渲染一個(gè) DOM 元素,也不會(huì)出現(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>
【條件判斷】
如果有多個(gè)條件性的子元素,<keep-alive> 要求同時(shí)只有一個(gè)子元素被渲染
<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> 樹內(nèi)的所有嵌套組件中觸發(fā)
<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 屬性允許組件有條件地緩存。二者都可以用逗號(hào)分隔字符串、正則表達(dá)式或一個(gè)數(shù)組來表示
<!-- 逗號(hào)分隔字符串 --> <keep-alive include="a,b"> <component :is="view"></component> </keep-alive> <!-- 正則表達(dá)式 (使用 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 選項(xiàng),如果 name 選項(xiàng)不可用,則匹配它的局部注冊(cè)名稱(父組件 components 選項(xiàng)的鍵值)。匿名組件不能被匹配
<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>
總結(jié)
以上所述是小編給大家介紹的Vue動(dòng)態(tài)組件實(shí)例解析,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
關(guān)于移動(dòng)端與大屏幕自適應(yīng)適配方案
這篇文章主要介紹了關(guān)于移動(dòng)端與大屏幕自適應(yīng)適配方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10vue單頁面應(yīng)用打開新窗口顯示跳轉(zhuǎn)頁面的實(shí)例
今天小編就為大家分享一篇vue單頁面應(yīng)用打開新窗口顯示跳轉(zhuǎn)頁面的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09vue 使用vant插件做tabs切換和無限加載功能的實(shí)現(xiàn)
這篇文章主要介紹了vue 使用vant插件做tabs切換和無限加載功能的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11vue實(shí)現(xiàn)路由跳轉(zhuǎn)動(dòng)態(tài)title標(biāo)題信息
這篇文章主要介紹了vue實(shí)現(xiàn)路由跳轉(zhuǎn)動(dòng)態(tài)title標(biāo)題信息,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06vue動(dòng)態(tài)設(shè)置img的src路徑實(shí)例
今天小編就為大家分享一篇vue動(dòng)態(tài)設(shè)置img的src路徑實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09vue如何設(shè)置輸入框只能輸入數(shù)字且只能輸入小數(shù)點(diǎn)后兩位,并且不能輸入減號(hào)
這篇文章主要介紹了vue如何設(shè)置輸入框只能輸入數(shù)字且只能輸入小數(shù)點(diǎn)后兩位,并且不能輸入減號(hào)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05vue通過定時(shí)器實(shí)現(xiàn)垂直滾動(dòng)公告
這篇文章主要為大家詳細(xì)介紹了vue通過定時(shí)器實(shí)現(xiàn)垂直滾動(dòng)公告,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04