一、父模块向子模块传值

//父html
<my-child [childdata]="parentdata"></my-child>

其中,my-child为子模块的selector标签,childdata为子模块声明的变量(用来保存父模块传递过来的值),parentdata为父模块声明的变量(保存着父模块的值)

子模块中引用父模块传过来的值:

//child.component.ts
import { Component, OnInit,Input } from '@angular/core';

@component({
  selector:'my-child',
  ...
})
export class ChildComponent implements OnInit{
  @Input() childdata:any;
  //Do some action with childdata
}

二、子模块向父模块传递事件

子模块传出事件:

//child.component.ts
import { Component, OnInit,Output } from '@angular/core';

@component({
  selector:'my-child',
  ...
})
export class ChildComponent implements OnInit{
  @OUtput() childevent=new EventEmitter();
  //Do some action
}

父html引用事件:

<my-child (childevent)="parentevent($event)"></my-child>

其中childevent为子模块事件,parentevent为父模块事件

 

相关文章:

  • 2021-07-27
  • 2022-12-23
  • 2021-05-23
  • 2021-09-21
  • 2021-07-02
  • 2021-11-21
  • 2021-11-19
  • 2021-05-21
猜你喜欢
  • 2022-12-23
  • 2021-07-19
  • 2022-12-23
  • 2021-09-05
  • 2022-12-23
  • 2022-01-07
  • 2022-12-23
相关资源
相似解决方案