首页 文章

如何从子组件向父组件发送通信?

提问于
浏览
0

我只是因为基础或 parent component 代码的大小和代码组织的增长而拆分组件 . 所以根据 parent component 的各个部分拆分组件 .

所以每个 child component 都是同一个 parent component 的每个部分 . 但我的问题是如何从 parent component 访问 child component 对象?因为我看到的大多数示例都是基于来自 parent component 的click事件来查看 child component (如对话框),并且该值将传递回父html click事件并使用emitter和output参数捕获值 .

而在我的情况下,没有这样的点击动作来触发 child component . 那么如何将 child component 中的值传递给 parent component

2 回答

  • 0

    在父组件中

    onDocumentSelect( documentData: IDocument) {
    console.log(documentData);
      }
    
    <app-document [showDocument] = 'displayDocument' (selectDocument)=onDocumentSelect($event)></app-document>
    

    在子组件中:

    @Output() selectDocument: EventEmitter<e.IDocument>;
    
      @Input() showDocument: boolean;
    

    我在函数中触发emit,计算DocumentSource的数据 .

    this.selectDocument.emit(this.DocumentSource.data);
    

    这里this.DocumentSource是dataTable,它在Child组件中具有值,需要在父组件中访问 .

    除此之外,我有另外一个不同类型字段的类似数据需要从子组件传递给父组件 .

    页面加载时我收到以下错误

    TypeError:无法读取undefined的属性'subscribe'

  • 0

    在上面的代码片段中,通过将类型更改为任何已解决的问题 .

    关于子组件:

    @Output() public selectDocument: EventEmitter<any> = new EventEmitter<any>();
    

    在父组件上:

    onDocumentSelect( documentData: any) {
    console.log(documentData);
      }
    

相关问题