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)的組件,顯示新的組件。
每一次切換,都需要銷毀+創(chuàng)建
但是這樣消耗有點(diǎn)大,所以我們?cè)谧咏M件中引用了v-once指令,這樣可以將顯示在頁(yè)面中的組件存到內(nèi)存中,不會(huì)完全銷毀。
<!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é)果:
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun測(cè)試上述代碼運(yùn)行效果。
希望本文所述對(duì)大家vue.js程序設(shè)計(jì)有所幫助。
相關(guān)文章:
1. html清除浮動(dòng)的6種方法示例2. phpstudy apache開(kāi)啟ssi使用詳解3. JavaScript中常見(jiàn)的幾種獲取元素的方式4. ASP.NET MVC使用異步Action的方法5. Xml簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理6. jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器7. jsp文件下載功能實(shí)現(xiàn)代碼8. uni-app結(jié)合.NET 7實(shí)現(xiàn)微信小程序訂閱消息推送9. AJAX的跨域問(wèn)題解決方案10. ajax實(shí)現(xiàn)頁(yè)面的局部加載
