一、實(shí)現(xiàn)兩個(gè)組件間互相展示、互相隱藏
創(chuàng)新互聯(lián)公司主要企業(yè)基礎(chǔ)官網(wǎng)建設(shè),電商平臺建設(shè),移動手機(jī)平臺,小程序開發(fā)等一系列專為中小企業(yè)按需網(wǎng)站設(shè)計(jì)產(chǎn)品體系;應(yīng)對中小企業(yè)在互聯(lián)網(wǎng)運(yùn)營的各種問題,為中小企業(yè)在互聯(lián)網(wǎng)的運(yùn)營中保駕護(hù)航。
<!DOCTYPE html> <html> <head> <title>動態(tài)組件</title> <script type="text/javascript" src="./vue-dev.js"></script> </head> <body> <div id="app"> <child-one v-if="type=='child-one'" content="child-one"></child-one> <child-two v-if="type=='child-two'" content="child-two"></child-two> <button @click="handleChangeEvent">change</button> </div> <script type="text/javascript"> Vue.component('child-one', { props: ["content"], template: `<div>{{content}}</div>`, }); Vue.component('child-two', { props: ["content"], template: `<div>{{content}}</div>`, }) var vm = new Vue({ el: '#app', data(){ return{ type:'child-one' } }, methods:{ handleChangeEvent:function(){ this.type= this.type=="child-one" ? 'child-two':'child-one'; } } }) </script> </body> </html>
頁面效果圖如下:
二、動態(tài)組件,簡化頁面代碼
使用:父組件 dom標(biāo)簽使用 ,對組件名稱進(jìn)行綁定
<div id="app"> <!-- <child-one v-if="type=='child-one'" content="child-one"></child-one> <child-two v-if="type=='child-two'" content="child-two"></child-two> --> <!--動態(tài)組件標(biāo)簽component 利用is接收指定標(biāo)簽組件--> <component :is="type" :content="type"></component> <button @click="handleChangeEvent">change</button> </div>
無論使用v-if還是components來使用動態(tài)組件的實(shí)現(xiàn),都是在點(diǎn)擊交互后,每一次頁面效果的切換,會自動銷毀前一個(gè)組件,再重新創(chuàng)建一個(gè)組件,頁面則顯示響應(yīng)的內(nèi)容, 這樣的實(shí)現(xiàn)方式是比較消耗性能的
三、 v-show和v-once
使用v-show,則會只是隱藏在dom元素中,組件都會被創(chuàng)建。
在子組件中,加入v-once,當(dāng)每次切換組件效果時(shí),不再需要每次都經(jīng)過創(chuàng)建-銷毀的過程,而是在內(nèi)存中直接取用上一次使用過的組件的內(nèi)容
Vue.component('child-one',{ template:'<div v-once>child-one</div>' }) Vue.component('child-two',{ template:'<div v-once>child-two</div>' })
使用v-once,可以有效提高靜態(tài)內(nèi)容的展示效率,提高性能
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
文章名稱:Vue動態(tài)組件components和v-once指令的實(shí)現(xiàn)
地址分享:http://aaarwkj.com/article44/iijgee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、App設(shè)計(jì)、域名注冊、網(wǎng)站制作、微信小程序、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)