子传父

119 阅读1分钟

子传父

1,在子组件里想应用的地方写this.emit("子组件的方法名字",需要传走的叁数) //子传父 建一个方法体 private addpeopleFunction() { //自己起的名字 this.emit("chilrenadd", this.tableData); }

2,在子组件上写@子组件的方法名字=父组件的方法名字 <AddPeople @chilrenadd="chilrenadd">

3在父组件里面写函数方法(父组件的方法名字) private chilrenadd(val: any) { const newPeopleData: any = []; val.forEach((element) => { newPeopleData.push({ commander: element.certificateNo, personId: element.personId, personName: element.personName, workerType: element.type, entityId:element.entityId, }); }); this.$emit("parent", newPeopleData); this.callBack({ show: false, type: 1 }); }

父传子

1在父组件引用子组件的地方 <:A=B>

A和子组件里面的prop中的值要对应

B是要给传给子组件里面的内容的 this.A去使用

21,在父组件的组件引用子组件的地方定义 < ref=A(方法名字)>

在需要调用调用子组件的地方应用 (A方法的名字 collectionoflawsRefFunction() { this.$emit("collectionoflawsFun"(方法名字在子组件上面绑定), this.checkListNew(传的叁数)) },)

在父组件里面要有子组件绑定的方法名字(collectionoflawsFun) this.$refs.A.B(父组件的方法名字)(叁数)

2,在子组件中要有方法B的名字