一、父组件给子组件传值

1 在父组件中,子组件通过属性接收父组件传递过来的参数

  1. //.html
  2. <app-header [title]="title"></app-header>

2 子组件中引入Input模块

  1. import { Component, OnInit,Input } from '@angular/core';

3 @Input注册接收数据

  1. export class HeaderComponent implements OnInit {
  2. @Input() title:string;
  3. constructor() { }
  4. ngOnInit() {
  5. }
  6. }

二、子组件给父组件传值

  1. //1.子组件通过属性接收父组件的方法
  2. <app-header [run]="run"></app-header>
  1. export class AppComponent {
  2. run(id:string){
  3. console.log(id);
  4. }
  5. }
  1. //2.子组件@Input接收,调用
  2. export class HeaderComponent implements OnInit {
  3. @Input() run:any;
  4. ...
  5. handleClick(){
  6. this.run("1234")
  7. }
  8. }
  1. <button (click)="handleClick()">向父传参</button>