vue學(xué)習(xí)筆記之動態(tài)組件和v-once指令簡單示例
本文實例講述了vue動態(tài)組件和v-once指令。分享給大家供大家參考,具體如下:
點擊按鈕時,自動切換兩個組件
<component :is='type'></component>,當(dāng)點擊按鈕之后,會自動清除原來的組件,顯示新的組件。
每一次切換,都需要銷毀+創(chuàng)建
但是這樣消耗有點大,所以我們在子組件中引用了v-once指令,這樣可以將顯示在頁面中的組件存到內(nèi)存中,不會完全銷毀。
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>動態(tài)組件和v-once指令</title> <script src='https://cdn.jsdelivr.net/npm/vue/dist/vue.js'></script></head><body><div id='app'> <component :is='type'></component><!-- <child-one v-if='type === ’child-one’'></child-one>--><!-- <child-two v-if='type === ’child-two’'></child-two>--> <button @click='handleBtnClick'>change</button></div></body></html><script> Vue.component(’child-one’, { template: ’<div v-once>child-one</div>’ }) Vue.component(’child-two’, { template: ’<div v-once>child-two</div>’ }) var vm = new Vue({ el: ’#app’, data: { type: ’child-one’ }, methods: { handleBtnClick: function () {this.type = (this.type === ’child-one’ ? ’child-two’ : ’child-one’); } } })</script>
運行結(jié)果:
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.jb51.net/code/HtmlJsRun測試上述代碼運行效果。
希望本文所述對大家vue.js程序設(shè)計有所幫助。
相關(guān)文章:
1. React+umi+typeScript創(chuàng)建項目的過程2. ASP調(diào)用WebService轉(zhuǎn)化成JSON數(shù)據(jù),附j(luò)son.min.asp3. php測試程序運行速度和頁面執(zhí)行速度的代碼4. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究5. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執(zhí)行過程解析6. 無線標(biāo)記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁7. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報錯問題分析8. ASP中常用的22個FSO文件操作函數(shù)整理9. SharePoint Server 2019新特性介紹10. 三個不常見的 HTML5 實用新特性簡介
