vue學(xué)習(xí)筆記之動(dòng)態(tài)組件和v-once指令簡(jiǎn)單示例
本文實(shí)例講述了vue動(dòng)態(tài)組件和v-once指令。分享給大家供大家參考,具體如下:
點(diǎn)擊按鈕時(shí),自動(dòng)切換兩個(gè)組件
<component :is='type'></component>,當(dāng)點(diǎn)擊按鈕之后,會(huì)自動(dòng)清除原來(lái)的組件,顯示新的組件。
每一次切換,都需要銷(xiāo)毀+創(chuàng)建
但是這樣消耗有點(diǎn)大,所以我們?cè)谧咏M件中引用了v-once指令,這樣可以將顯示在頁(yè)面中的組件存到內(nèi)存中,不會(huì)完全銷(xiāo)毀。
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>動(dòng)態(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>
運(yùn)行結(jié)果:
感興趣的朋友可以使用在線(xiàn)HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun測(cè)試上述代碼運(yùn)行效果。
希望本文所述對(duì)大家vue.js程序設(shè)計(jì)有所幫助。
相關(guān)文章:
1. 淺談XML Schema中的elementFormDefault屬性2. 父div高度不能自適應(yīng)子div高度的解決方案3. 淺談?dòng)蓀osition屬性引申的css進(jìn)階討論4. 選擇模式 - XSL教程 - 25. 利用XMLSerializer將對(duì)象串行化到XML6. 阿里前端開(kāi)發(fā)中的規(guī)范要求7. XML和YAML的使用方法8. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執(zhí)行過(guò)程解析9. 三個(gè)不常見(jiàn)的 HTML5 實(shí)用新特性簡(jiǎn)介10. 詳解CSS故障藝術(shù)
