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跟组件里演示,会报错


 

 

 

posted on 2019-05-21 14:27  LoaderMan  阅读(2284)  评论(0编辑  收藏  举报

导航