简体   繁体   English

对我来说,从服务将数据推送到组件的最佳方法是什么?

[英]What is the best way for me to push data to a component from a service?

In my angular project I currently have a service that uses http calls to retrieve data from my java code. 在我的有角项目中,我目前有一个使用http调用从我的Java代码中检索数据的服务。 Every 10 seconds the service calls the Java side and gets new data from it. 该服务每10秒钟调用一次Java端,并从中获取新数据。 I now have another component that needs the data in my service. 现在,我有另一个组件需要服务中的数据。 I need this component to have a field called 'data' that just gets updated automatically when the service gets new information. 我需要此组件具有一个名为“数据”的字段,该字段在服务获取新信息时会自动更新。

How can I set them up so that the service pushes the new information to my other component ? 如何设置它们,以便服务将新信息推送到其他组件 I would like to be able to use {{data}} in my component's html and have that be automatically updated without having to reload the page. 我希望能够在组件的html中使用{{data}}并自动更新它们,而不必重新加载页面。

My component does have the service 'Autowired' in already. 我的组件确实已经具有“自动接线”服务。 So currently I can just call 'this.data = this.service.getData()' but that call is within my ngOnInit method so it only happens once, and the data field does not get updated when the service's data field gets updated. 因此,当前我只能调用“ this.data = this.service.getData()”,但该调用在我的ngOnInit方法中,因此仅发生一次,并且在更新服务的数据字段时不会更新数据字段。

You can create a messaging service that publishes data for subscribers or implement this functionality in your original service. 您可以创建一个消息服务,为订阅者发布数据,或在原始服务中实现此功能。

I would suggest having a separate messaging service and have relevant components or services publish/subscribe to it. 我建议拥有一个单独的消息传递服务,并向其发布/订阅相关的组件或服务。

messaging.service.ts messaging.service.ts

import { Injectable } from '@angular/core';
import { Subject }    from 'rxjs/Subject';


@Injectable()
export class MessagingService {
  private sharedValue = new Subject<string>();  

  // Observable string streams
  sharedValue$ = this.sharedValue.asObservable();

    // Service message commands
  publishData(data: string) {
    this.sharedValue.next(data);
  }

You would inject the service like this: 您将像这样注入服务:

constructor(private messagingService: MessagingService ) {}

Publish to the service: 发布到服务:

this.messagingService.publishData(sharedValue);

Subscribe to the service: 订阅服务:

this.messagingService.sharedValue$.subscribe(
            data => {                    
                this.localSharedValue = data;
            });

FROM ANSWER BELOW BY: DeborahK (who's courses on Pluralsight everyone should watch) 下一篇:DeborahK(谁应该关注谁的Pluralsight课程)

Actually, all you need is a getter. 实际上,您所需要的只是吸气剂。

Change your 'data' property to a getter and that will do the trick: 将您的“数据”属性更改为吸气剂,即可达到目的:

 get data(): any { return this.service.getData(); } 

Angular change detection will detect any time that data is changed in the service which will cause it to re-evaluate its bindings and call this getter to re-get the data. 角度更改检测将检测服务中数据的任何更改时间,这将导致它重新评估其绑定并调用此getter来重新获取数据。

No need for a fancy service or Subject. 无需花哨的服务或主题。 :-) :-)

Actually, all you need is a getter. 实际上,您所需要的只是吸气剂。

Change your 'data' property to a getter and that will do the trick: 将您的“数据”属性更改为吸气剂,即可达到目的:

get data(): any {
    return this.service.getData();
}

Angular change detection will detect any time that data is changed in the service which will cause it to re-evaluate its bindings and call this getter to re-get the data. 角度更改检测将检测服务中数据的任何更改时间,这将导致它重新评估其绑定并调用此getter来重新获取数据。

No need for a fancy service or Subject . 不需要花哨的服务或Subject :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ReactJS-在数组中将数据推送到数组中的最佳方法是什么? - ReactJS - What is the best way to push data in an array within an array? 使用钩子在嵌套对象中推送新数据的最佳方法是什么? - What is the best way to push new data in nested oject using hooks? 从另一个组件有条件地导入组件的最佳方法是什么? - What's the best way to conditionally import a component from another component? Angular 5 Service正在从api返回数据,但Component没有向我显示数据 - Angular 5 Service is returning data from api , but Component is not showing me the data 从子表单组件传递和检索数据的最佳方法是什么? - What's the best way to pass and retrieve data from a child form component? 在 vue 3 中重置组件的反应数据的最佳方法是什么? - What is the best way to reset a component's reactive data in vue 3? 使用javascript从html表单在Google表格中推送数据的最佳方法 - Best way to push data in a Google Sheet from html form with javascript 从AJAX附加数据的最佳方法是什么? - What is the best way to append data from AJAX? 将数据从http服务器端推送到浏览器客户端的最佳方法 - Best way to push data from http server side to browser client 从父组件触发子组件的最佳方式是什么? - What's the best way to trigger a child emit from the parent component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM