溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

淺析vue component 組件使用

發(fā)布時(shí)間:2020-09-19 18:12:36 來(lái)源:腳本之家 閱讀:117 作者:v折耳 欄目:web開(kāi)發(fā)

component 使用

component的注冊(cè)

1.全局注冊(cè)

使用用Vue.component('componentName',{template:'<div class="tem1">hello world</div>'})在初始化實(shí)例之前。

componentName自定義名稱(chēng)

在實(shí)例聲明的作用域下中使用<componentName></componentName> 成功渲染效果就是 '<div class="tem1">hello world</div>

Vue.component('my-component',{ 
      template:'<div class="tem1">hello world</div>' 
    }  

組件中的數(shù)據(jù)使用 

component不能使用實(shí)例的data ,但是可以在component 使用data 聲明變量,data的使用只能使用函數(shù)形式

Vue.component('my-component',{ 
      template:'<div class="tem1">hello world {{comdata}}</div>', 
      data:function(){return {comdata:'hehe'}} 
    });

2.局部主持

在實(shí)例化的new Vue 中設(shè)置components

var app=new Vue({ 
      el:'#app', 
      components:{'example':{template:'<div>children template</div>'}} 
    }) 

組件中的數(shù)據(jù)使用

var app=new Vue({ 
      el:'#app', 
      data:{num:220}, 
      components:{ 
        'example':{ 
          template:'<div>children template{{mydata}}</div>', 
          data:function(){return {mydata:' mydata=data'};} 
        } 
 
      } 
    })

注意:組件不能使用實(shí)例的data:{num:220}的參數(shù)會(huì)報(bào)錯(cuò)

組件中同樣支持methods、computed等其他屬性 不會(huì)沖突保持相對(duì)的獨(dú)立

這時(shí)候就必須考慮不同組件的數(shù)據(jù)通信問(wèn)題

vue js總結(jié)來(lái)說(shuō)通過(guò)props down events up 來(lái)傳輸數(shù)據(jù)

給父級(jí)調(diào)用數(shù)據(jù)使用props聲明,給子集調(diào)用使用events來(lái)聲明

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

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI