一、 父组件给子组件传值 -@Input
1. 父组件调用子组件的时候传入数据
ts里声明msg
export class AboutPage {
public msg:string;
........
}
html里
<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>
本文介绍如何在Angular项目中实现父组件向子组件传递数据的方法。具体步骤包括:父组件通过属性绑定的方式传递数据,子组件使用@Input装饰器接收数据,并在模板中展示接收到的数据。
612

被折叠的 条评论
为什么被折叠?



