Vue父子模版?zhèn)髦导敖M件傳值的三種方法
這里是針對(duì)于vue1.0,如果要學(xué)2.0,建議大家去看官方文檔
vue2.0 http://vuefe.cn/guide/
vue-router2.0https://router.vuejs.org/zh-cn/essentials/getting-started.html
第一種
<div id="example">
<my-component></my-component>
</div>
<script src="../node_modules/vue/dist/vue.js"></script>
<script>
//向子組件傳遞數(shù)據(jù)
//省略extend方法,vue內(nèi)部調(diào)用
Vue.component('my-component', {
//模板里不支持駝峰的屬性寫(xiě)法,需要轉(zhuǎn)換為‘-'連接的屬性寫(xiě)法
data:function(){
return{
parentMsg: '雨歇微涼'
}
},
template: '<div>'
+'<input v-model="parentMsg">'
+'<br>'
+'<child-component :my-message="parentMsg"></child-component>'
+'</div>',
components: {
'child-component': {
props: ['myMessage'],
template: '<div>{{myMessage}}</div>'
}
}
});
// 創(chuàng)建根實(shí)例1
new Vue({
el: '#example'
});
</script>
有什么疑惑的,也可以去查官網(wǎng)的文檔,prop傳值,這里也可以直接拷去試,如果你有什么更好的簡(jiǎn)介,還希望能夠拿出來(lái)分享。
第二種
<div id="example">
<my-component></my-component>
</div>
<script src="../node_modules/vue/dist/vue.js"></script>
<script>
//向子組件傳遞數(shù)據(jù)
//省略extend方法,vue內(nèi)部調(diào)用
Vue.component('my-component', {
data:function(){
return {
name:'xiaoming',
age:20
}
},
//模板里不支持駝峰的屬性寫(xiě)法,需要轉(zhuǎn)換為‘-'連接的屬性寫(xiě)法
template: '<div >{{name}}Parent</div><child1-component v-bind:msg-name="name"></child1-component>',
components: {
'child1-component': {
// 聲明 props
props: ['msgName'],
template: '<div>A child-111111 component!{{msgName}}</div>'
}
}
});
// 創(chuàng)建根實(shí)例1
new Vue({
el: '#example'
});
</script>
第三種
<div id="example">
<my-component></my-component>
</div>
<script src="../node_modules/vue/dist/vue.js"></script>
<script>
//向子組件傳遞數(shù)據(jù)
//省略extend方法,vue內(nèi)部調(diào)用
Vue.component('my-component', {
data:function(){
return {
name:'xiaoming',
age:20
}
},
//模板里不支持駝峰的屬性寫(xiě)法,需要轉(zhuǎn)換為‘-'連接的屬性寫(xiě)法
template: '<div >{{name}}Parent</div><child1-component some="1 + 1"></child1-component><child2-component :some="1 + 3"></child2-component>',
components: {
'child1-component': {
// 聲明 props
props: ['some'],
template: '<div>{{some}}</div>',
ready:function(){
console.log(this.some)
}
},
'child2-component': {
// 聲明 props
props: ['some'],
template: '<div>{{some}}</div>',
ready:function(){
console.log(this.some)
}
}
}
});
// 創(chuàng)建根實(shí)例1
new Vue({
el: '#example'
});
</script>
這個(gè)例子主要是說(shuō)明帶冒號(hào)和不帶冒號(hào)的區(qū)別,不帶冒號(hào)就是一個(gè)字符串死值,帶冒號(hào)會(huì)到父模版的data中去尋找值的具體內(nèi)容。
總結(jié)
以上所述是小編給大家介紹的Vue父子模版?zhèn)髦导敖M件傳值的三種方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
vue elementUI實(shí)現(xiàn)自定義正則規(guī)則驗(yàn)證
本文主要介紹了vue elementUI實(shí)現(xiàn)自定義正則規(guī)則驗(yàn)證,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
vue+Element-ui前端實(shí)現(xiàn)分頁(yè)效果
這篇文章主要為大家詳細(xì)介紹了vue+Element-ui前端實(shí)現(xiàn)分頁(yè)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
vue各種事件監(jiān)聽(tīng)實(shí)例(小結(jié))
這篇文章主要介紹了vue各種事件監(jiān)聽(tīng)實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

