How to call router outlet child component method from parent comonent

前端 未结 10 2007
甜味超标
甜味超标 2020-12-18 19:42

I am new to angular2


    

I have a button in parent component, if

10条回答
  •  没有蜡笔的小新
    2020-12-18 19:52

    With child in router-outlet, you can use ContentChild to be able to call a method in the child. So...

    import { ContentChild } from '@angular/core';
    

    in your parent:

    @ContentChild(ChildComponent)
    private childComponent: ChildComponent;
    

    and on your click event do:

    this.childComponent.doSomething()
    

    Also you need to add your child component in the providers array in parent:

    @Component({
      selector: 'parent',
      ...
      providers: [ChildComponent]
    })
    

提交回复
热议问题