1.子调用父的方法
子组件
父组件
2.使用emitter实现孙子传爷
孙子组件
TypeScript
import emitter from '@ohos.events.emitter';
let event: emitter.InnerEvent = {
eventId: 1,
priority: emitter.EventPriority.HIGH
};
let eventData: emitter.EventData = {
data: {
"state": true,
"item": JSON.stringify(this.item)
}
};
// 孙子组件发送事件
emitter.emit(event, eventData);
爷爷组件
TypeScript
aboutToAppear(): void {
let event: emitter.InnerEvent = {
eventId: 1
};
let callback = (eventData: emitter.EventData): void => {
if (eventData && eventData.data) {
this.item = JSON.parse(eventData.data.item)
this.isShow = eventData.data.state
}
};
// 爷爷组件监听事件
emitter.on(event, callback);
}