Vue 動態(tài)組件與 v-once 指令的實現(xiàn)
本文介紹了Vue 動態(tài)組件與 v-once 指令的實現(xiàn),分享給大家,具體如下:
<div id="root"> <child-one></child-one> <child-two></child-two> <button>change</button> </div> Vue.component('child-one', { template: `<div>child-one</div>`, }) Vue.component('child-two', { template: `<div>child-two</div>`, }) let vm = new Vue({ el: '#root' })
上面代碼需實現(xiàn),當(dāng)點擊按鈕時,child-one和child-two實現(xiàn)toggle效果,該怎么實現(xiàn)呢?
<div id="root"> <child-one v-if="type === 'child-one'"></child-one> <child-two v-if="type === 'child-two'"></child-two> <button @click="handleBtnClick">change</button> </div> Vue.component('child-one', { template: `<div>child-one</div>`, }) Vue.component('child-two', { template: `<div>child-two</div>`, }) let vm = new Vue({ el: '#root', data: { type:'child-one' }, methods: { handleBtnClick(){ this.type = this.type === 'child-one' ? 'child-two' : 'child-one' } } })
通過上面handleBtnClick函數(shù)的實現(xiàn),配合v-if指令就能實現(xiàn)toggle效果
動態(tài)組件
下面這段代碼實現(xiàn)的效果和上面是一樣的。
<div id="root"> <component :is="type"></component> //is內(nèi)容的變化,會自動的加載不同的組件 <button @click="handleBtnClick">change</button> </div> Vue.component('child-one', { template: `<div>child-one</div>`, }) Vue.component('child-two', { template: `<div>child-two</div>`, }) let vm = new Vue({ el: '#root', data: { type:'child-one' }, methods: { handleBtnClick(){ this.type = this.type === 'child-one' ? 'child-two' : 'child-one' } } })
動態(tài)組件的意思是它會根據(jù)is里面數(shù)據(jù)的變化,會自動的加載不同的組件
v-noce指令
每次點擊按鈕切換的時候,Vue 底層會幫我們干什么呢?Vue 底層會判斷這個child-one組件現(xiàn)在不用了,取而代之要用child-two組件,然后它就會把child-one組件銷毀掉,在創(chuàng)建一個child-two組件。假設(shè)這時child-two組件要隱藏,child-one組件要顯示,這個時候要把剛剛創(chuàng)建的child-two銷毀掉,在重新創(chuàng)建child-one組件,也就是每一次切換的時候,底層都是要銷毀一個組件,在創(chuàng)建一個組件,這種操作會消耗一定的性能。如果我們的組件的內(nèi)容,每次都是一樣的可以在上面加一個v-once,看下面代碼。
Vue.component('child-one', { template: `<div v-once>child-one</div>`, }) Vue.component('child-two', { template: `<div v-once>child-two</div>`, })
加上v-once指令之后,有什么好處呢?當(dāng)chlid-one組件第一次被渲染時,因為組件上面有一個v-once指令,所以它直接就放到內(nèi)存里了,當(dāng)切換的時候child-two組件第一次被渲染時,它也會被放到內(nèi)存里,再次點擊切換時,這時并不需要再重新創(chuàng)建一個child-one組件了,而是從內(nèi)存里直接拿出以前的child-one組件,它的性能會更高一些。
所以在 Vue 當(dāng)中,通過v-once指令,可以提高一些靜態(tài)內(nèi)容的展示效率
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
element表格el-table實現(xiàn)虛擬滾動解決卡頓問題
當(dāng)頁面數(shù)據(jù)過多,前端渲染大量的DOM時,會造成頁面卡死問題,本文主要介紹了element表格el-table實現(xiàn)虛擬滾動解決卡頓問題,具有一定的參考價值,感興趣的可以了解一下2023-10-10vue elementui el-form rules動態(tài)驗證的實例代碼詳解
在使用elementUI el-form 中,對于業(yè)務(wù)不同的時候可能會產(chǎn)生不同表單結(jié)構(gòu),但是都是存在同一個表單控件el-form中。這篇文章主要介紹了vue elementui el-form rules動態(tài)驗證的實例代碼,需要的朋友可以參考下2019-05-05vue3之Suspense加載異步數(shù)據(jù)的使用
本文主要介紹了vue3之Suspense加載異步數(shù)據(jù)的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Vue導(dǎo)入Echarts實現(xiàn)折線散點圖
這篇文章主要為大家詳細(xì)介紹了Vue導(dǎo)入Echarts實現(xiàn)折線散點圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10Vue3使用Echarts導(dǎo)致tooltip失效問題及解決方法
Vue3 使用 proxy 對象代理,而 echarts 則使用了大量的全等(===), 對比失敗從而導(dǎo)致了bug,這篇文章主要介紹了Vue3使用Echarts導(dǎo)致tooltip失效問題及解決方法,需要的朋友可以參考下2023-08-08