Angular父子组件数据传递方法有哪些?

更新于
2026-09-25 04:14:54
1阅读来源:SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1603个文字,预计阅读时间需要7分钟。

Angular父子组件数据传递方法有哪些?

Angular中父子组件间如何传递数据?

以下章节将介绍如何在Angular中实现父子组件间的数据传递,包括从父组件向子组件传递数据以及从子组件向父组件传递数据的方法。希望对大家有所帮助!

环境:Angular CLI: 11.0.6

1. 从父组件向子组件传递数据

方法一:使用属性(@Input)

在父组件中,首先需要定义一个属性并将其传递给子组件:

typescript// 父组件import { Component } from '@angular/core';

@Component({ selector: 'app-parent', template: ` `})export class ParentComponent { message='Hello from Parent!';}

在子组件中,使用`@Input()`装饰器接收父组件传递的属性:

typescript// 子组件import { Component, Input } from '@angular/core';

@Component({ selector: 'app-child', template: ` {{ childMessage }} `})export class ChildComponent { @Input() childMessage: string;}

方法二:使用事件(@Output)

在父组件中,可以使用`@Output()`装饰器定义一个事件,并在子组件中触发该事件:

typescript// 父组件import { Component, Output, EventEmitter } from '@angular/core';

@Component({ selector: 'app-parent', template: ` `})export class ParentComponent { message='Hello from Parent!';

handleChildEvent(event: any) { console.log(event); }}

在子组件中,触发父组件定义的事件:

typescript// 子组件import { Component, Output, EventEmitter } from '@angular/core';

@Component({ selector: 'app-child', template: ` `})export class ChildComponent { @Output() childEvent=new EventEmitter();

Angular父子组件数据传递方法有哪些?

sendMessage() { this.childEvent.emit(this.message); }}

2. 从子组件向父组件传递数据

方法一:使用属性(@Output)

在子组件中,使用`@Output()`装饰器定义一个事件,并在父组件中监听该事件:

typescript// 子组件import { Component, Output, EventEmitter } from '@angular/core';

@Component({ selector: 'app-child', template: ` `})export class ChildComponent { @Output() childEvent=new EventEmitter();

sendMessage() { this.childEvent.emit('Hello from Child!'); }}

在父组件中,监听子组件触发的事件:

typescript// 父组件import { Component, Input, Output, EventEmitter } from '@angular/core';

@Component({ selector: 'app-parent', template: ` `})export class ParentComponent { message='Hello from Parent!';

handleChildEvent(event: string) { console.log(event); }}

方法二:使用服务(Service)

创建一个服务,用于在父子组件间传递数据:

typescript// 服务import { Injectable } from '@angular/core';

@Injectable({ providedIn: 'root'})export class DataService { private message: string;

constructor() {}

setMessage(message: string) { this.message=message; }

getMessage(): string { return this.message; }}

在父组件中,注入服务并设置消息:

typescript// 父组件import { Component, OnInit } from '@angular/core';import { DataService } from './data.service';

@Component({ selector: 'app-parent', template: ` `})export class ParentComponent implements OnInit { constructor(private dataService: DataService) {}

ngOnInit() { this.dataService.setMessage('Hello from Parent!'); }}

在子组件中,注入服务并获取消息:

typescript// 子组件import { Component, OnInit } from '@angular/core';import { DataService } from './data.service';

@Component({ selector: 'app-child', template: ` {{ message }} `})export class ChildComponent implements OnInit { message: string;

constructor(private dataService: DataService) {}

ngOnInit() { this.message=this.dataService.getMessage(); }}

以上是Angular中父子组件间传递数据的方法,希望对大家有所帮助!

Angular中父子组件间怎么传递数据?下面本篇文章就来给大家介绍一下Angular中父组件向子组件传数据、子组件向父组件传数据的方法,希望对大家有所帮助!

环境:

  • Angular CLI: 11.0.6
  • Angular: 11.0.7
  • Node: 12.18.3
  • npm : 6.14.6
  • IDE: Visual Studio Code

组件之间传递数据,最主要的就是父子组件之间传递数据, 例如:

<parent-component> <child-component></child-component> </parent-component>

父组件传入数据给子组件,同时,子组件数据发生变化是,希望能够通知父组件。

Angular 中,@Input() 和 @Output() 为子组件提供了一种与其父组件通信的方法。 @Input() 允许父组件更新子组件中的数据。相反,@Output() 允许子组件向父组件发送数据。

父传子 @Input()

1. 子组件定义@Input()

子组件中的 @Input() 装饰器表示该属性可以从其父组件中获取值。

例如:

export class ChildComponent { @Input() message: string; }

  • 增加@Input() 装饰器的变量,除了数据可以从父组件传入后,其他逻辑和普通变量一致;

  • 子组件的html代码中,既可使用message这个变量, 例如:

<p> Parent says: {{message}} </p>

2. 父组件传递变量给子组件

当父组件调用子组件时,可以把父组件的变量(如messageToChild) 传递给子组件

<child-component [message]="messageToChild"></child-component>

子组件中,可以更改message这个传入的变量,但是其作用域只在子组件中,父组件拿不到更改后的结果。(如何传给父组件,请接着看)

子传父 @Output()

Angular通过事件(Event)来实现子组件通知父组件数据的改变,父组件需要订阅该事件。

1. 子组件定义@Output

子组件定义@Output

export class ChildComponent { // EventEmitter ,这意味着它是一个事件 // new EventEmitter<string>() - // 使用 Angular 来创建一个新的事件发射器,它发出的数据是 string 类型的。 @Output() newItemEvent = new EventEmitter<string>(); addNewItem(value: string) { this.newItemEvent.emit(value); } }

子组件当数据发生变化时,调用这个addNewItem方法既可。例如,html中

<label>Add an item: <input #newItem></label> <button (click)="addNewItem(newItem.value)">Add to parent's list</button>

2. 父组件订阅事件

1、父组件的ts代码中,增加一个处理上面事件的方法,例如

addItem(newItem: string) { // logic here }

2、父组件的html中,订阅该事件。

<child-component (newItemEvent)="addItem($event)"></child-component>

事件绑定 (newItemEvent)='addItem($event)' 会把子组件中的 newItemEvent 事件连接到父组件的 addItem() 方法。

总结

  • 使用@Input() 和 @Output() 可以很方便的实现父子组件之间的数据传递、共享。

  • 可以同时使用 @Input() 和 @Output()

更多编程相关知识,请访问:编程学习!!

以上就是聊聊Angular中父子组件间怎么传递数据的详细内容,更多请关注自由互联其它相关文章!

本文共计1603个文字,预计阅读时间需要7分钟。

Angular父子组件数据传递方法有哪些?

Angular中父子组件间如何传递数据?

以下章节将介绍如何在Angular中实现父子组件间的数据传递,包括从父组件向子组件传递数据以及从子组件向父组件传递数据的方法。希望对大家有所帮助!

环境:Angular CLI: 11.0.6

1. 从父组件向子组件传递数据

方法一:使用属性(@Input)

在父组件中,首先需要定义一个属性并将其传递给子组件:

typescript// 父组件import { Component } from '@angular/core';

@Component({ selector: 'app-parent', template: ` `})export class ParentComponent { message='Hello from Parent!';}

在子组件中,使用`@Input()`装饰器接收父组件传递的属性:

typescript// 子组件import { Component, Input } from '@angular/core';

@Component({ selector: 'app-child', template: ` {{ childMessage }} `})export class ChildComponent { @Input() childMessage: string;}

方法二:使用事件(@Output)

在父组件中,可以使用`@Output()`装饰器定义一个事件,并在子组件中触发该事件:

typescript// 父组件import { Component, Output, EventEmitter } from '@angular/core';

@Component({ selector: 'app-parent', template: ` `})export class ParentComponent { message='Hello from Parent!';

handleChildEvent(event: any) { console.log(event); }}

在子组件中,触发父组件定义的事件:

typescript// 子组件import { Component, Output, EventEmitter } from '@angular/core';

@Component({ selector: 'app-child', template: ` `})export class ChildComponent { @Output() childEvent=new EventEmitter();

Angular父子组件数据传递方法有哪些?

sendMessage() { this.childEvent.emit(this.message); }}

2. 从子组件向父组件传递数据

方法一:使用属性(@Output)

在子组件中,使用`@Output()`装饰器定义一个事件,并在父组件中监听该事件:

typescript// 子组件import { Component, Output, EventEmitter } from '@angular/core';

@Component({ selector: 'app-child', template: ` `})export class ChildComponent { @Output() childEvent=new EventEmitter();

sendMessage() { this.childEvent.emit('Hello from Child!'); }}

在父组件中,监听子组件触发的事件:

typescript// 父组件import { Component, Input, Output, EventEmitter } from '@angular/core';

@Component({ selector: 'app-parent', template: ` `})export class ParentComponent { message='Hello from Parent!';

handleChildEvent(event: string) { console.log(event); }}

方法二:使用服务(Service)

创建一个服务,用于在父子组件间传递数据:

typescript// 服务import { Injectable } from '@angular/core';

@Injectable({ providedIn: 'root'})export class DataService { private message: string;

constructor() {}

setMessage(message: string) { this.message=message; }

getMessage(): string { return this.message; }}

在父组件中,注入服务并设置消息:

typescript// 父组件import { Component, OnInit } from '@angular/core';import { DataService } from './data.service';

@Component({ selector: 'app-parent', template: ` `})export class ParentComponent implements OnInit { constructor(private dataService: DataService) {}

ngOnInit() { this.dataService.setMessage('Hello from Parent!'); }}

在子组件中,注入服务并获取消息:

typescript// 子组件import { Component, OnInit } from '@angular/core';import { DataService } from './data.service';

@Component({ selector: 'app-child', template: ` {{ message }} `})export class ChildComponent implements OnInit { message: string;

constructor(private dataService: DataService) {}

ngOnInit() { this.message=this.dataService.getMessage(); }}

以上是Angular中父子组件间传递数据的方法,希望对大家有所帮助!

Angular中父子组件间怎么传递数据?下面本篇文章就来给大家介绍一下Angular中父组件向子组件传数据、子组件向父组件传数据的方法,希望对大家有所帮助!

环境:

  • Angular CLI: 11.0.6
  • Angular: 11.0.7
  • Node: 12.18.3
  • npm : 6.14.6
  • IDE: Visual Studio Code

组件之间传递数据,最主要的就是父子组件之间传递数据, 例如:

<parent-component> <child-component></child-component> </parent-component>

父组件传入数据给子组件,同时,子组件数据发生变化是,希望能够通知父组件。

Angular 中,@Input() 和 @Output() 为子组件提供了一种与其父组件通信的方法。 @Input() 允许父组件更新子组件中的数据。相反,@Output() 允许子组件向父组件发送数据。

父传子 @Input()

1. 子组件定义@Input()

子组件中的 @Input() 装饰器表示该属性可以从其父组件中获取值。

例如:

export class ChildComponent { @Input() message: string; }

  • 增加@Input() 装饰器的变量,除了数据可以从父组件传入后,其他逻辑和普通变量一致;

  • 子组件的html代码中,既可使用message这个变量, 例如:

<p> Parent says: {{message}} </p>

2. 父组件传递变量给子组件

当父组件调用子组件时,可以把父组件的变量(如messageToChild) 传递给子组件

<child-component [message]="messageToChild"></child-component>

子组件中,可以更改message这个传入的变量,但是其作用域只在子组件中,父组件拿不到更改后的结果。(如何传给父组件,请接着看)

子传父 @Output()

Angular通过事件(Event)来实现子组件通知父组件数据的改变,父组件需要订阅该事件。

1. 子组件定义@Output

子组件定义@Output

export class ChildComponent { // EventEmitter ,这意味着它是一个事件 // new EventEmitter<string>() - // 使用 Angular 来创建一个新的事件发射器,它发出的数据是 string 类型的。 @Output() newItemEvent = new EventEmitter<string>(); addNewItem(value: string) { this.newItemEvent.emit(value); } }

子组件当数据发生变化时,调用这个addNewItem方法既可。例如,html中

<label>Add an item: <input #newItem></label> <button (click)="addNewItem(newItem.value)">Add to parent's list</button>

2. 父组件订阅事件

1、父组件的ts代码中,增加一个处理上面事件的方法,例如

addItem(newItem: string) { // logic here }

2、父组件的html中,订阅该事件。

<child-component (newItemEvent)="addItem($event)"></child-component>

事件绑定 (newItemEvent)='addItem($event)' 会把子组件中的 newItemEvent 事件连接到父组件的 addItem() 方法。

总结

  • 使用@Input() 和 @Output() 可以很方便的实现父子组件之间的数据传递、共享。

  • 可以同时使用 @Input() 和 @Output()

更多编程相关知识,请访问:编程学习!!

以上就是聊聊Angular中父子组件间怎么传递数据的详细内容,更多请关注自由互联其它相关文章!