Angular 4发出和订阅共享服务中的事件

编程入门 行业动态 更新时间:2024-10-28 06:35:32
本文介绍了Angular 4发出和订阅共享服务中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我要在我的主要组件中发出一个事件:

I am emitting an event in my main component:

mainponent.ts

mainponent.ts

this.sharedService.cartData.emit(this.data);

这是我的sharedService.ts

Here is my sharedService.ts

import { Component, Injectable, EventEmitter } from '@angular/core'; export class SharedService { cartData = new EventEmitter<any>(); }

在我的其他(子)组件中,我想访问此值,但是以某种方式,订阅不起作用:

In my other (Sub) Component, I want to access this value, but somehow, the subscription does not work:

dashboard.ts

dashboard.ts

private myData: any; constructor(private sharedService: SharedService) { this.sharedService.cartData.subscribe( (data: any) => myData = data, error => this.errorGettingData = <any>error, () => this.aggregateData(this.myData)); }

我错过了什么吗?当我将数据作为Injectable传递时,它工作正常. 在一些REST调用之后发生(在主组件中)发出事件.

Am I missing something? It works fine when I pass the data as an Injectable. Emitting the event (in the main component) happens after some REST calls.

更新

因此,问题在于子组件是在第一次发出事件之后创建的.我想在这种情况下,最好将数据直接注入subcompnent.

So the problem is that the Subcomponent is created after the first emit of the event. I guess in this case it is better to inject the data into the subcompnent directly.

推荐答案

更新: Plunker示例不再维护,请使用StackBlitz 这里的例子 stackblitz/edit/stackoverflow-questions-45351598-angular?file = src%2Fapp%2Fappponent.ts

Update: Plunker example no longer maintained please use StackBlitz example here stackblitz/edit/stackoverflow-questions-45351598-angular?file=src%2Fapp%2Fappponent.ts

我已经使用您上面提供的代码创建了一个有效的插件示例. plnkr.co/edit/LS1uqB?p=preview

I have created a working plunker example using the code you provided above. plnkr.co/edit/LS1uqB?p=preview

import { Component, NgModule, Injectable, EventEmitter, AfterViewInit } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; @Injectable() export class SharedService { cartData = new EventEmitter<any>(); } @Component({ selector: 'app-app', template: ` <h1> Main Component <button (click)="onEvent()">onEvent</button> </h1> <p> <app-dashboard></app-dashboard> </p> `, }) export class App implements AfterViewInit { data: any = "Shared Data"; constructor(private sharedService: SharedService) { } ngAfterViewInit() { this.sharedService.cartData.emit("ngAfterViewInit: " + this.data); } onEvent() { this.sharedService.cartData.emit("onEvent: " + this.data); } } @Component({ selector: 'app-dashboard', template: ` <h2> Dashboard component </h2> <p> {{myData}} </p> `, }) export class AppDashboard implements AfterViewInit { myData: any; constructor(private sharedService: SharedService) { this.sharedService.cartData.subscribe( (data: any) => { console.log(data); this.myData = data; }); } } @NgModule({ imports: [ BrowserModule ], declarations: [ App, AppDashboard ], providers: [ SharedService ], bootstrap: [ App ] }) export class AppModule {}

在此处查看生命周期挂钩 angular.io/guide/lifecycle-hooks

View lifecycle hooks here angular.io/guide/lifecycle-hooks

更多推荐

Angular 4发出和订阅共享服务中的事件

本文发布于:2023-11-26 16:09:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634350.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事件   Angular

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!