Angular 父子组件传值
Angular 父子组件传值 @Input @Output @ViewChild
新建一个头部组件 newsheader
在主组件引用 news 组件,在news组件添加 newsheader 组件。
设置newsheader组件样式
设置newsheader组件的内容,添加一个class属性
<h2 class="header">这是一个头部组件</h2>
如果需要全局设置,则在 style.css 中设置。
如果单独设置自己的,则在自己组件的css中设置。
此项目案例设置全局的。
/* You can add global styles to this file, and also import other style files */ .header{ height: 44px; line-height: 44px; text-align: center; background-color: #000; color: #fff; text-align: center; }
把新闻页面的数据传给头组件(父组件向子组件传值) @Input
首先在新闻界组件定义一个数据(在父组件定义一个数据)
在父组件中创建一个变量,用于传递给子组件:
public message = "这是新闻组件的MSG"
这个 message 属性属于新闻组件(父组件),我们可以在新闻组件上打印出来。
<app-newsheader></app-newsheader> <hr> 这是新闻组件 ----- {{message}} <hr> <br>
在头部组件(子组件)中并没有定义 message 属性,我们在头部(子组件)是拿不到数据的,他们数据不能共享,因此我们需要通过父组件把需要的值(message)传给子组件。
1.父组件调用子组件的时候传入数据
<app-newsheader [msg]="message"></app-newsheader>
2.子组件引入 Input 模块
import { Component, OnInit,Input } from '@angular/core';
3.接收父组件传进的数据
@Input() msg:string; /**通过Input接收父组件传进的msg */
4.在头部(子组件)使用父组件传进的数据 msg
<h2 class="header">这是一个头部组件 -- {{msg}}</h2>
如果多个变量就添加多个HTML属性
假如 父组件 ts 文件有两个属性需要传给子组件
父组件调用子组件的HTML代码也传入两个属性
子组件在去接收父组件传进的两个值
子组件就可以使用了
子组件执行父组件的方法 @Input
创建一个新的组件 home
创建一个新的组件 footer
通过 home 组件和 footer 组件来讲解子组件执行父组件的方法。
在 根组件 引入 home 组件,在 home组件 引入 footer 组件。
1.在home组件(父组件)创建一个 run 方法
ts文件
import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-home', templateUrl: './home.component.html', styleUrls: ['./home.component.css'] }) export class HomeComponent implements OnInit { public msg = "我是home组件的msg" constructor() { } ngOnInit() { } run(){ alert('这是home组件的run方法') } }
html 文件
<app-footer [msg]="msg" [run]="run"></app-footer> <br> <hr> home组件<br> <button (click)="run()">点击执行run方法</button>
2.子组件接收父组件传进的数据和方法
ts 文件
import { Component, OnInit,Input } from '@angular/core'; @Component({ selector: 'app-footer', templateUrl: './footer.component.html', styleUrls: ['./footer.component.css'] }) export class FooterComponent implements OnInit { @Input() msg:string; @Input() run; constructor() { } ngOnInit() { } }
html 文件
<h2 class="header">footer子组件 -- {{msg}}</h2> <br> <button (click)="run()">点击执行run方法</button>
父组件接收子组件返回的数据
在父组件创建一个方法,用于接收子组件数据
// 接收子组件的数据 getDataFromChild(childData){ alert(childData) }
把 getDataFromChild 方法在调用子组件的时候传给子组件
<app-footer [msg]="msg" [run]="run" [getDataFromChild]="getDataFromChild"></app-footer>
<br>
<hr>
home组件<br>
<button (click)="run()">点击执行run方法</button>
子组件接收父组件传进来的方法,并且创建方法返回给父组件数据
import { Component, OnInit,Input } from '@angular/core'; @Component({ selector: 'app-footer', templateUrl: './footer.component.html', styleUrls: ['./footer.component.css'] }) export class FooterComponent implements OnInit { @Input() msg:string; @Input() run; @Input() getDataFromChild; public msginfo = '这是子组件的数据' constructor() { } ngOnInit() { } sendParent(){ //子组件自己的方法 this.getDataFromChild(this.msginfo) //子组件调用父组件的方法 } }
给子组件添加一个按钮给父组件传数据
<h2 class="header">footer子组件 -- {{msg}}</h2> <br> <button (click)="run()">点击执行run方法</button> <button (click)="sendParent()">点击,给父组件传值</button>
子组件用 @Output 的方法执行父组件方法
使用最开始的 news 组件 和 newsheader 组件。
子组件引入 Output 和 EventEmitter
import { Component, OnInit ,Input,Output,EventEmitter} from '@angular/core';
子组件中实例化 EventEmitter
@Output() private outer=new EventEmitter<string>(); /*用 EventEmitter 和 output 装饰器配合使用 <string>指定类型变量*/
子组件通过 EventEmitter 对象 outer 实例广播数据
sendParent(){ // alert('zhixing'); this.outer.emit('msg from child') }
父组件调用子组件的时候,定义接收事件 , outer 就是子组件的 EventEmitter 对象 outer
<app-header (outer)="runParent($event)"></app-header>
父组件接收到数据会调用自己的 runParent 方法,这个时候就能拿到子组件的数据
//接收子组件传递过来的数据 runParent(msg:string){ alert(msg); }
父组件主动获取子组件的属性和方法
定义 footer 组件
export class FooterComponent implements OnInit { public msg:string; constructor() { } ngOnInit() { } footerRun(){ alert('这是 footer 子组件的 Run 方法'); } }
父组件调用子组件的时候给子组件起个名字
<app-footer #footer></app-footer>
直接获取执行子组件的方法
<button (click)='footer.footerRun()'>获取子组件的数据</button>
父组件通过局部变量获取子组件的引用,通过 ViewChild 主动获取子组件的数据和方法
调用子组件给子组件定义一个名称
<app-footer #footerChild></app-footer>
引入 ViewChild
import { Component, OnInit ,ViewChild} from '@angular/core';
ViewChild 和刚才的子组件关联起来
@ViewChild('footerChild') footer;
调用子组件
run(){ this.footer.footerRun();
}
【版权声明】本博文著作权归作者所有,任何形式的转载都请联系作者获取授权并注明出处!
【重要说明】博文仅作为本人的学习记录,论点和观点仅代表个人而不代表技术的真理,目的是自我学习和有幸成为可以向他人分享的经验,因此有错误会虚心接受改正,但不代表此刻博文无误!
【博客园地址】叫我+V : http://www.cnblogs.com/wjw1014
【CSDN地址】叫我+V : https://wjw1014.blog.csdn.net/
【Gitee地址】叫我+V :https://gitee.com/wjw1014
【重要说明】博文仅作为本人的学习记录,论点和观点仅代表个人而不代表技术的真理,目的是自我学习和有幸成为可以向他人分享的经验,因此有错误会虚心接受改正,但不代表此刻博文无误!
【博客园地址】叫我+V : http://www.cnblogs.com/wjw1014
【CSDN地址】叫我+V : https://wjw1014.blog.csdn.net/
【Gitee地址】叫我+V :https://gitee.com/wjw1014
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!