亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue 動(dòng)態(tài)組件components和v-once指令的實(shí)現(xiàn)

 更新時(shí)間:2019年08月30日 08:21:15   作者:ALLen、LAS  
這篇文章主要介紹了Vue 動(dòng)態(tài)組件components和v-once指令的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

一、實(shí)現(xiàn)兩個(gè)組件間互相展示、互相隱藏

<!DOCTYPE html>
<html>
<head>
  <title>動(dòng)態(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>

頁(yè)面效果圖如下:

 

二、動(dòng)態(tài)組件,簡(jiǎn)化頁(yè)面代碼

使用:父組件 dom標(biāo)簽使用 ,對(duì)組件名稱進(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> -->

     <!--動(dòng)態(tài)組件標(biāo)簽component 利用is接收指定標(biāo)簽組件-->
    <component :is="type" :content="type"></component>
    <button @click="handleChangeEvent">change</button>
  </div>

無(wú)論使用v-if還是components來(lái)使用動(dòng)態(tài)組件的實(shí)現(xiàn),都是在點(diǎn)擊交互后,每一次頁(yè)面效果的切換,會(huì)自動(dòng)銷毀前一個(gè)組件,再重新創(chuàng)建一個(gè)組件,頁(yè)面則顯示響應(yīng)的內(nèi)容, 這樣的實(shí)現(xiàn)方式是比較消耗性能的

三、 v-show和v-once

使用v-show,則會(huì)只是隱藏在dom元素中,組件都會(huì)被創(chuàng)建。

在子組件中,加入v-once,當(dāng)每次切換組件效果時(shí),不再需要每次都經(jīng)過(guò)創(chuàng)建-銷毀的過(guò)程,而是在內(nèi)存中直接取用上一次使用過(guò)的組件的內(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)容的展示效率,提高性能

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論