angular中父组件给子组件传值-@input

1. 父组件调用子组件的时候传入数据

<app-header [msg]="msg"></app-header>

2. 子组件引入 Input 模块

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

3. 子组件中 @Input 接收父组件传过来的数据

export class HeaderComponent implements OnInit {
  @Input() msg:string
  constructor() { }
  ngOnInit() {
  }
}

4. 子组件中使用父组件的数据

h2>这是头部组件--{{msg}}</h2>

注意:不要在app跟组件里演示,会报错


 

原文地址:https://www.cnblogs.com/loaderman/p/10899618.html