angular2 call function of parent component

前端 未结 4 842
有刺的猬
有刺的猬 2021-01-30 01:33

I have an app where I have an upload component where I can upload a file. It is embedded in the body.component.

On upload, it should use a function (e.g.

4条回答
  •  情歌与酒
    2021-01-30 01:57

    I would create a custom event in the child component. Something like that:

    @Component({
      selector: 'child-comp',
      (...)
    })
    export class ChildComponent {
      @Output()
      uploaded = new EventEmitter();
    
      uploadComplete() {
        this.uploaded.emit('complete');
      }
    

    Your parent component could register on this event

    @Component({
      template `
        
      `,
      directives: [ ChildComponent ]
    })
    export class ParentComponent {
      (...)
    
      someMethod(event) {
      }
    }
    

    Another way would be inject the parent component in the child one but they will be strongly linked together...

提交回复
热议问题