typescript 将数据添加到行为对象数组 Angular 5 的末尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49525637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
add data to the end of a behavior object array Angular 5
提问by Smokey Dawson
I have some data that I want to be shared with my entire app so I have created a service like so..
我有一些数据想要与我的整个应用程序共享,所以我创建了一个这样的服务..
user.service
用户服务
userDataSource = BehaviorSubject<Array<any>>([]);
userData = this.userDataSource.asObservable();
updateUserData(data) {
this.userDataSource.next(data);
}
then in my component Im getting some data from an api and then sending that data to userDataSource
like so..
然后在我的组件中,我从 api 获取一些数据,然后将这些数据发送给userDataSource
这样..
constructor(
private: userService: UserService,
private: api: Api
){
}
ngOnInit() {
this.api.getData()
.subscribe((data) => {
this.userService.updateUserData(data);
})
}
now that all works but.. I want to be able to add data to the end of the array inside the userDataSource
so basically the equivalent of a .push
am I able to just call the updateUserData()
function and add more data or will doing that overwrite what is currently in there?
现在一切正常,但是..我希望能够将数据添加到数组的末尾,userDataSource
所以基本上相当于.push
我能够调用该updateUserData()
函数并添加更多数据,或者这样做会覆盖当前的内容那里?
Any help would be appreciated
任何帮助,将不胜感激
回答by Arun Redhu
You can add a new method to your service like addData
in which you can combine your previous data with new data like.
您可以为您的服务添加一种新方法,例如addData
您可以将以前的数据与新数据相结合。
import {Injectable} from '@angular/core';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
@Injectable()
export class UserService {
userDataSource: BehaviorSubject<Array<any>> = new BehaviorSubject([]);
userData = this.userDataSource.asObservable();
updateUserData(data) {
this.userDataSource.next(data);
}
addData(dataObj) {
const currentValue = this.userDataSource.value;
const updatedValue = [...currentValue, dataObj];
this.userDataSource.next(updatedValue);
}
}
回答by G. Siganos
For someone that may come accross this issue with a BehaviorSubject<YourObject[]>
.
对于可能会遇到此问题的人BehaviorSubject<YourObject[]>
。
I found in this articlea way to properly add the new array of YourObject
我在这篇文章中找到了一种正确添加新数组的方法YourObject
import { Observable, BehaviorSubject } from 'rxjs';
import { YourObject} from './location';
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class ObjService {
private theObjData: BehaviorSubject<YourObject[]> = new BehaviorSubject<YourObject[]>(null);
constructor() {
}
public SetObjData(newValue: YourObject[]): void {
this.theObjData.next(Object.assign([], newValue));
}
}
How to update data:
如何更新数据:
// inside some component
this.api.userData().subscribe((results:YourObject) =>
this.objService.SetObjData(results);
)
How to observe changes on other component
如何观察其他组件的变化
// inside another component
ngOnInit() {
this.objService.GetAccountStatements().subscribe((results) =>
...
)
}
回答by joh04667
Normally Observables
and Subject
s are meant to be streamsof data, not an assignment of data. BehaviorSubject
s are different because they hold their last emitted value.
通常Observables
和Subject
s 是数据流,而不是数据分配。BehaviorSubject
s 是不同的,因为它们保存着最后发出的值。
Normally Subject
s or BehaviorSubjects
inside of a contained class (like a Service) do not want to expose themselves publicly to any other classes, so it's best practice to access their properties with get
ters or methods. This keeps the data stream cold to all subscribers.
通常Subject
s 或BehaviorSubjects
包含的类(如服务)不希望将自己公开给任何其他类,因此最佳实践是使用get
ter 或方法访问它们的属性。这使所有订阅者的数据流保持冷态。
However, since the BehaviorSubject
holds the last emitted value, there's a few options here. If allsubscribers need a concatenated stream of data from every emission, you could access the last emitted value and append to it:
但是,由于BehaviorSubject
保存了最后发出的值,因此这里有几个选项。如果所有订阅者都需要来自每次发射的串联数据流,您可以访问最后发射的值并将其附加到它:
userDataSource = BehaviorSubject<any[]>([]);
userData = this.userDataSource.asObservable();
updateUserData(data) {
this.userDataSource.next(this.userDataSource.value.push(data));
}
...or, in what might be considered better practice, Subscribers to this Subject could do their own transformation on the stream:
...或者,在可能被认为是更好的实践中,该主题的订阅者可以在流上进行自己的转换:
this.api.userData()
.scan((prev, current) => prev.push(current). [])
.subscribe((data) => {
this.concatenatedUserData = data;
});