Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular rxjs-如何强制调用可观察对象?_Angular_Rxjs - Fatal编程技术网

Angular rxjs-如何强制调用可观察对象?

Angular rxjs-如何强制调用可观察对象?,angular,rxjs,Angular,Rxjs,我在页面加载时调用了以下内容 combineLatest([ this.baseball.getPlayers(), this.baseball.getTeams(), ]).pipe(.....) 然而,我从一个子系统发射回这个父系统,并希望再次调用上面的代码。我猜我必须使用一个主题,但不确定如何使用。我该怎么做 emitCalled() { //call the above code } 您是对的,您可以使用subject来触发对上述内容进行重新

我在页面加载时调用了以下内容

combineLatest([
      this.baseball.getPlayers(),
      this.baseball.getTeams(),
    ]).pipe(.....)
然而,我从一个子系统发射回这个父系统,并希望再次调用上面的代码。我猜我必须使用一个主题,但不确定如何使用。我该怎么做

emitCalled() {
     //call the above code
}

您是对的,您可以使用subject来触发对上述内容进行重新评估

triggerSubject$ = new BehaviourSubject('');
myObservable$ = combineLatest([
  this.baseball.getPlayers(),
  this.baseball.getTeams(),
]).pipe(.....)
myUpdatedObservable$ = this.triggerSubject$.asObservable().pipe(
  mergeMap(() => this.myObservable$)
)

emitCalled() {
   this.triggerSubject$.next(null)
}
你想在哪里重新评估可观察到的东西

triggerSubject$ = new BehaviourSubject('');
myObservable$ = combineLatest([
  this.baseball.getPlayers(),
  this.baseball.getTeams(),
]).pipe(.....)
myUpdatedObservable$ = this.triggerSubject$.asObservable().pipe(
  mergeMap(() => this.myObservable$)
)

emitCalled() {
   this.triggerSubject$.next(null)
}
在html中

<ng-container *ngIf='myUpdatedObservable$ | async as myUpdatedObservable'>
  // myUpdatedObservable will contain the updated result here
</ng-container>

//myUpdatedObservable将在此处包含更新的结果

注意:我正在使用
行为主题
来确保应用程序加载时评估的可观察对象

您是对的,您可以使用主题来触发上述重新评估

triggerSubject$ = new BehaviourSubject('');
myObservable$ = combineLatest([
  this.baseball.getPlayers(),
  this.baseball.getTeams(),
]).pipe(.....)
myUpdatedObservable$ = this.triggerSubject$.asObservable().pipe(
  mergeMap(() => this.myObservable$)
)

emitCalled() {
   this.triggerSubject$.next(null)
}
你想在哪里重新评估可观察到的东西

triggerSubject$ = new BehaviourSubject('');
myObservable$ = combineLatest([
  this.baseball.getPlayers(),
  this.baseball.getTeams(),
]).pipe(.....)
myUpdatedObservable$ = this.triggerSubject$.asObservable().pipe(
  mergeMap(() => this.myObservable$)
)

emitCalled() {
   this.triggerSubject$.next(null)
}
在html中

<ng-container *ngIf='myUpdatedObservable$ | async as myUpdatedObservable'>
  // myUpdatedObservable will contain the updated result here
</ng-container>

//myUpdatedObservable将在此处包含更新的结果

注意:我正在使用
Behavior subject
来确保在应用程序加载时对可观察对象进行评估

关于使用对象,请尝试以下方法:

import { BehaviorSubject } from 'rxjs';

emitSubject = new BehaviorSubject(null);
.....
combineLatest([
      this.baseball.getPlayers(),
      this.baseball.getTeams(),
      this.emitSubject.asObservable(),
    ]).pipe(.....)

emitCalled() {
     this.emitSubject.next(null);
}

这样的办法应该行得通。我在手机上,所以我可能犯了错误

=======编辑========

要在每次调用emit时进行新的API调用,需要使用
switchMap

import { BehaviorSubject } from 'rxjs';
import { switchMap } from 'rxjs/operators';

emitSubject = new BehaviorSubject(null);
.....
// emitSubject is the initiation of the chain
this.emitSubject.asObservable().pipe(
  // switch to this observable every time emit subject gets a new value
  switchMap(_ => combineLatest([
      this.baseball.getPlayers(),
      this.baseball.getTeams(),
    ])),
).pipe(........)


emitCalled() {
     this.emitSubject.next(null);
}

使用主题是正确的,请尝试以下方法:

import { BehaviorSubject } from 'rxjs';

emitSubject = new BehaviorSubject(null);
.....
combineLatest([
      this.baseball.getPlayers(),
      this.baseball.getTeams(),
      this.emitSubject.asObservable(),
    ]).pipe(.....)

emitCalled() {
     this.emitSubject.next(null);
}

这样的办法应该行得通。我在手机上,所以我可能犯了错误

=======编辑========

要在每次调用emit时进行新的API调用,需要使用
switchMap

import { BehaviorSubject } from 'rxjs';
import { switchMap } from 'rxjs/operators';

emitSubject = new BehaviorSubject(null);
.....
// emitSubject is the initiation of the chain
this.emitSubject.asObservable().pipe(
  // switch to this observable every time emit subject gets a new value
  switchMap(_ => combineLatest([
      this.baseball.getPlayers(),
      this.baseball.getTeams(),
    ])),
).pipe(........)


emitCalled() {
     this.emitSubject.next(null);
}

这起作用了。但是,您知道为什么我的视图在发布后没有更新吗?控制台显示新结果,我正在视图中使用同步管道。您需要订阅更新的observable。我已经更新了解决方案,这是有效的。但是,您知道为什么我的视图在发布后没有更新吗?控制台显示新结果,我正在视图中使用同步管道。您需要订阅更新的observable。我已经更新了解决方案我没有看到我的API调用在emit上进行。我已经进行了编辑,希望对您有用。我没有看到我的API调用在emit上进行。我已经进行了编辑,希望对您有用。