一、父组件给子组件传值
1 在父组件中,子组件通过属性接收父组件传递过来的参数
//.html<app-header [title]="title"></app-header>
2 子组件中引入Input模块
import { Component, OnInit,Input } from '@angular/core';
3 @Input注册接收数据
export class HeaderComponent implements OnInit { @Input() title:string; constructor() { } ngOnInit() { }}
二、子组件给父组件传值
//1.子组件通过属性接收父组件的方法<app-header [run]="run"></app-header>
export class AppComponent { run(id:string){ console.log(id); }}
//2.子组件@Input接收,调用export class HeaderComponent implements OnInit { @Input() run:any; ... handleClick(){ this.run("1234") }}
<button (click)="handleClick()">向父传参</button>