淺析vue component 組件使用
component 使用
component的注冊
1.全局注冊
使用用Vue.component('componentName',{template:'<div class="tem1">hello world</div>'})在初始化實例之前。
componentName自定義名稱
在實例聲明的作用域下中使用<componentName></componentName> 成功渲染效果就是 '<div class="tem1">hello world</div>
Vue.component('my-component',{
template:'<div class="tem1">hello world</div>'
}
組件中的數(shù)據(jù)使用
component不能使用實例的data ,但是可以在component 使用data 聲明變量,data的使用只能使用函數(shù)形式
Vue.component('my-component',{
template:'<div class="tem1">hello world {{comdata}}</div>',
data:function(){return {comdata:'hehe'}}
});
2.局部主持
在實例化的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'};}
}
}
})
注意:組件不能使用實例的data:{num:220}的參數(shù)會報錯
組件中同樣支持methods、computed等其他屬性 不會沖突保持相對的獨立
這時候就必須考慮不同組件的數(shù)據(jù)通信問題
vue js總結(jié)來說通過props down events up 來傳輸數(shù)據(jù)
給父級調(diào)用數(shù)據(jù)使用props聲明,給子集調(diào)用使用events來聲明
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue2中,根據(jù)list的id進(jìn)入對應(yīng)的詳情頁并修改title方法
今天小編就為大家分享一篇vue2中,根據(jù)list的id進(jìn)入對應(yīng)的詳情頁并修改title方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
vue報錯Failed to execute 'appendChild&apos
這篇文章主要為大家介紹了vue報錯Failed to execute 'appendChild' on 'Node'解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
vue-image-crop基于Vue的移動端圖片裁剪組件示例
這篇文章主要介紹了vue-image-crop基于Vue的移動端圖片裁剪組件示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
vue.js實現(xiàn)的經(jīng)典計算器/科學(xué)計算器功能示例
這篇文章主要介紹了vue.js實現(xiàn)的經(jīng)典計算器/科學(xué)計算器功能,具有基本四則運(yùn)算計算器以及科學(xué)計算器的功能,可實現(xiàn)開方、乘方、三角函數(shù)以及公式運(yùn)算等功能,需要的朋友可以參考下2018-07-07

