How to call header component function to another component in angular 2?

后端 未结 5 935
别跟我提以往
别跟我提以往 2021-01-22 07:24

I want to call showmodel(displayType) from another compoent.How to call header component function to another component?

header.compoent.ts

5条回答
  •  [愿得一人]
    2021-01-22 08:13

    You will need to use EventEmitter.

    @component({
      selector:'app-header',
    })
    export class HeaderComponent {
    
      public showmodel(displayType): void {
            this.visible = true;                    
            this.visibleAnimate = true
      }
    
    }
    

    Now say in second component you emit the event on a button click.

    @component({
      selector:'another component',
      template: ``
    )
    export class com2{
      @Output() evt = new EventEmitter();
      callShowModel(){
         this.evt.emit();
      }
    }
    

    Now your event can be hooked up in parent component

    
    

提交回复
热议问题