国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Vue 動態組件components和v-once指令的實現

2019-11-19 10:56:25
字體:
來源:轉載
供稿:網友

一、實現兩個組件間互相展示、互相隱藏

<!DOCTYPE html><html><head>  <title>動態組件</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>

頁面效果圖如下:

 

二、動態組件,簡化頁面代碼

使用:父組件 dom標簽使用 ,對組件名稱進行綁定

 <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> -->     <!--動態組件標簽component 利用is接收指定標簽組件-->    <component :is="type" :content="type"></component>    <button @click="handleChangeEvent">change</button>  </div>

無論使用v-if還是components來使用動態組件的實現,都是在點擊交互后,每一次頁面效果的切換,會自動銷毀前一個組件,再重新創建一個組件,頁面則顯示響應的內容, 這樣的實現方式是比較消耗性能的

三、 v-show和v-once

使用v-show,則會只是隱藏在dom元素中,組件都會被創建。

在子組件中,加入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,可以有效提高靜態內容的展示效率,提高性能

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 石阡县| 隆化县| 宽甸| 道孚县| 县级市| 宝应县| 邢台市| 洛川县| 榆树市| 封丘县| 霍山县| 鲁山县| 前郭尔| 普兰店市| 云梦县| 哈尔滨市| 枣阳市| 门头沟区| 沧源| 弋阳县| 册亨县| 磐安县| 乃东县| 霍林郭勒市| 丹寨县| 广安市| 民勤县| 城步| 汕尾市| 鹤峰县| 莆田市| 双城市| 新巴尔虎右旗| 沭阳县| 开平市| 黄平县| 越西县| 大城县| 大连市| 嘉善县| 台山市|