我正在使用rxjs BehaviorSubject创建服务,在从Component1发送值并在Component3中订阅时,我得到Component3中的值,这是实现的
`import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
@Injectable()
export class UserService {
constructor() { }
// Observable navItem source
public _navItemSource = new BehaviorSubject(undefined);
// Observable navItem stream
navItem$ = this._navItemSource.asObservable();
// service command
changeNav(nu) {
this._navItemSource.next([...nu]);
}
}`
`this._userService.changeNav(self.notificationslist);`
`this.subscription = this._userService.navItem$.subscribe(item =>
this.item1 = item);`
现在我想在另一个组件(Component2)中使用相同的服务将一些不同的值发送到同一个组件Component3
`this._userService.changeNav(this.updatedFlag);`
`this.subscription = this._userService.navItem$.subscribe(item =>
this.item1 = item);`
然后在Component3上订阅它时,我应该从Component1和Component2获取所有值。 所以我更新了我的服务文件,但是我收到以下错误
caused by: this._navItemSource.asObservable(...).scan is not a function
因为我是rxjs的新手,请纠正我的错误。
`import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
@Injectable()
export class UserService {
constructor() {
}
// Observable navItem source
public _navItemSource = new BehaviorSubject(undefined);
// Observable navItem stream
navItem$ = this._navItemSource.asObservable().scan((acc, one) =>(acc +
one));
// service command
changeNav(nu) {
this._navItemSource.next([...nu]);
}
}`
答案 0 :(得分:1)
确保导入扫描操作符。并且在扫描函数中合并您的结果,您可以返回带有前一个和新值发出的JSON
Microsoft.Xrm.Sdk.Client.ServiceProxy<TService>