1.父子组件通信
(1).Props 和 Events:父组件通过props向子组件传递数据,子组件通过$emit触发事件向父组件传递数据。
示例:
父组件:
<template>
<child-component :parentStrData="parStr" :parentArrData="parArr" :parentObjData="parObj" :parentFunction="parFun" @childHandle="getChildVal" />
</template>
<script>
import ChildComponent from './ChildComponent.vue'
export default {
components: {
ChildComponent
},
data() {
return {
parMessage: '我是父组件的属性',
parStr: 'hello word',
parArr: [1,2,3],
parObj: { name: '小明', age: 18 }
}
},
methods: {
getChildVal(message) {
console.log('子级传递过来的值:',message) // 输出:子级传递过来的值:我是子组件的数据
// 调用父组件方法 ...
},
parFun(){
console.log("父组件把这个方法传入子组件中,在子组件里可以直接调用。")
},
getParent() {
console.log('父组件方法');
}
}
}
</script>
子组件:
子组件主动获取父组件中的数据和方法
this.$

最低0.47元/天 解锁文章
1058

被折叠的 条评论
为什么被折叠?



