Vue父子模版?zhèn)髦导敖M件傳值的三種方法
這里是針對于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', {
//模板里不支持駝峰的屬性寫法,需要轉(zhuǎn)換為‘-'連接的屬性寫法
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)建根實例1
new Vue({
el: '#example'
});
</script>
有什么疑惑的,也可以去查官網(wǎng)的文檔,prop傳值,這里也可以直接拷去試,如果你有什么更好的簡介,還希望能夠拿出來分享。
第二種
<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
}
},
//模板里不支持駝峰的屬性寫法,需要轉(zhuǎn)換為‘-'連接的屬性寫法
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)建根實例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
}
},
//模板里不支持駝峰的屬性寫法,需要轉(zhuǎn)換為‘-'連接的屬性寫法
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)建根實例1
new Vue({
el: '#example'
});
</script>
這個例子主要是說明帶冒號和不帶冒號的區(qū)別,不帶冒號就是一個字符串死值,帶冒號會到父模版的data中去尋找值的具體內(nèi)容。
總結(jié)
以上所述是小編給大家介紹的Vue父子模版?zhèn)髦导敖M件傳值的三種方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
vue elementUI實現(xiàn)自定義正則規(guī)則驗證
本文主要介紹了vue elementUI實現(xiàn)自定義正則規(guī)則驗證,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03

