我几乎是 rxjs 的菜鸟,可能只是完全使用了错误的功能。本质上,我正在做一堆 .NET Core 3.1 后端并模拟两秒的延迟。我两次使用相同的服务调用来模拟类似于我在生产代码上所做的事情。因此,我的代码即使正在接线,在概念上也非常相似。当两个服务调用完成时,我希望微调器停止。我希望它们同时启动,但我不在乎它们是可观察的还是实际的项目并且同时进行呼叫。无论我尝试过什么,我都会看到微调器保持打开状态,并且“假”设置的上下文不断发生。
零件:
import { Component, OnInit } from '@angular/core';
import { combineLatest, Observable, of } from 'rxjs';
import { Entry } from '../Models/entry.model';
import { TestService } from '../test.service';
@Component({
selector: 'app-Entry',
templateUrl: './Entry.component.html',
styleUrls: ['./Entry.component.scss']
})
export class EntryComponent implements OnInit {
entry$: Observable<Entry> = this.service.getItem();
entry2$: Observable<Entry> = this.service.getItem();
loading$: Observable<boolean> = of(true);
constructor(private readonly service: TestService) { }
ngOnInit() {
//my guess is this part is completely wrong for what I am trying to accomplish.
combineLatest([this.entry$, this.entry2$]).pipe(x => this.loading$ = of(false));
}
}
看法:
<mat-spinner [hidden]="loading$ | async"></mat-spinner>
<p>
<em>{{entry$ | async | json}}</em>
</p>
<p>
<em>{{entry2$ | async | json}}</em>
</p>
整个项目在 GitHub 上并公开:https ://github.com/djangojazz/AngularMaterial 。您只需在 Visual Studio 中运行 API,然后使用“npm run start”启动 Angular 客户端。