rxjs 相关问题

JavaScript的Reactive Extensions(RxJS)是一组库,用于使用可观察集合和Array Extras样式组合来编写异步和基于事件的程序。

RxJSskipWhile 与过滤器

skipWhile 和过滤器运算符有什么区别? 常量源=间隔(1000); const 示例 = source.pipe(skipWhile(val => val < 5)); const subscribe = example.subscribe(val ...

回答 4 投票 0

Angular 成员序列

Angular 风格指南指出公共属性应该位于私有属性之前。但。如果我有一个想要公开的私人主题,我该如何在不破坏这个的情况下做到这一点

回答 1 投票 0

错误 TS2769:没有重载与使用来自 RXJS Angular 17 的 scan() 的此调用相匹配

我正在学习 Deborah Kurata 的 Angular RXJS 课程,我遇到了一个类型错误,我无法确定其中一种类型,并且它抛出了一个错误。这是使用 scan() 的操作流...

回答 1 投票 0

为什么 Rxjs switchMap 只输出 of() 源 observable 的最后一个值?

下面是 2 个片段,第一个是 map,第二个是 switchMap。 地图的工作原理是可以理解的: of('foo', '酒吧') .pipe(map((val) => 消毒剂(val))) .subscribe((val) => console.log('

回答 2 投票 0

我希望一个 Http 调用在下一个 Angular 调用之前完成

这是我的Http请求 在 getAllUserData() 中,我希望 this.getAnXSRfToken() 在继续处理 http 请求之前完成 exe; 获取所有用户数据() { //获取XSRF令牌 this.getAnXSRfTo...

回答 1 投票 0

在将错误和响应转发给 rxjs lastValueFrom 中的订阅者之前,我应该如何处理错误和响应(take(1))

这是我的代码 - 异步 getItemById(idParam: string): Promise { 返回等待lastValueFrom(this.http.get('http://localhost:3000/api/item?id=' + idParam).pipe(take(1))) } ...

回答 1 投票 0

如何并行执行多个 API 调用并在响应到达 Angular 中的 HTTP 服务时处理响应?

我有一个 HTTP 服务,用于接收需要由多个其他 API 使用的响应。我想并行执行这些 API 调用。此外,我不想等待所有 API 都完成

回答 1 投票 0

NgRx:在单个效果中调度多个操作

我需要在调用 API 请求后分派多个操作。 我目前正在使用此代码在 API 请求完成后分派一个操作: 更改状态$ = createEffect(() =>...

回答 6 投票 0

Angular 自动完成 - 如何推迟 API 数据请求直到用户聚焦该字段?

我正在一个项目中使用 Angular Material Autocomplete 来获取可能非常大(数千行)的网站列表。 我知道 startWith 正在触发 100% 的初始负载...

回答 3 投票 0

rxjs 仅在第一次执行 tap

我只想在获得第一个发出的值时执行tap() 就像是: 可观察的 。管道( tap(() => { /* 仅当我获得第一个发出的值时执行 */ }) ) .订阅((...

回答 8 投票 0

行为主体初始值为空?

私人客户:Subject = new BehaviourSubject(null); setCustomer(id, accountClassCode) { this.customer.next({'id': id, 'accountClassCode': accountClassCode}); } private customer: Subject<Object> = new BehaviorSubject<Object>(null); setCustomer(id, accountClassCode) { this.customer.next({'id': id, 'accountClassCode': accountClassCode}); } getCustomer() { return this.customer.asObservable(); } 我正在使用这部分代码,但收到错误,无法找到 null 的 id。有什么办法可以得到不为空的初始值吗? BehaviorSubject的目的是提供初始值。它可以是 null 或其他任何内容。如果无法提供有效的初始值(当用户 ID 未知时),则不应使用它。 ReplaySubject(1) 提供类似的行为(在订阅时发出最后一个值),但在使用 next 设置之前没有初始值。 应该是这样 private customer: Subject<Object> = new ReplaySubject<Object>(1); 由于对象可以为 null,所以更好的选择是像这样推断类型 private customer = new BehaviorSubject<Customer|null>(null); 尝试以这种方式构建您的服务: 服务: @Injectable() export class MyService { customerUpdate$: Observable<any>; private customerUpdateSubject = new Subject<any>(); constructor() { this.customerUpdate$ = this.customerUpdateSubject.asObservable(); } updatedCustomer(dataAsParams) { this.customerUpdateSubject.next(dataAsParams); } } 记得将 MyService 添加到提供商。 在更新客户端时(如果是这种情况),您可以执行以下操作: 组件(触发的组件): constructor(private myService: MyService) { // I'll put this here, it could go anywhere in the component actually // We make things happen, Client has been updated // We store client's data in an object this.updatedClient = this.myObjectWithUpdatedClientData; // Obj or whatever you want to pass as a parameter this.myService.updatedCustomer(this.updatedClient); } 组件(已订阅的组件): this.myService.customerUpdate$.subscribe((updatedClientData) => { // Wow! I received the updated client's data // Do stuff with this data! } ); 据我了解,您正在尝试将数据从一个组件传递到另一个组件。您获取客户的数据并通过您的应用程序将其发送到另一个组件,对吧?这就是我发布此解决方案的原因。 如果您对其他类型的订阅感兴趣,请阅读以下内容: Angular 2 特殊 Observables(Subject / Behaviour subject / ReplaySubject) 另一种方法是使用管道进行过滤,直到收到非空值。也可以用 takeWhile 来完成。 .pipe(filter(val => !!val)).subscribe(x => {}); 我发现很多情况下,我想要 ReplaySubject(1) 的简单性,但也想要 value 的 BehaviorSubject 属性,即它存储您最近的检索值,而无需订阅。考虑到需要广播初始值,使用 BehaviorSubject 总是很烦人,这是我很少想要的情况。为此,我创建了自己的CurrentSubject。 import { ReplaySubject } from "rxjs"; export class CurrentSubject<T> extends ReplaySubject<T> { value: T; set(value?: T) { this.value = value; this.next(value); } } 我想重写 next 方法,但由于某种原因我无法让它工作,所以我选择了一个名为 set 的方法。我的覆盖尝试看起来像这样...... export class CurrentSubject<T> extends ReplaySubject<T> { value: T; next(value?: T) { this.value = value; super.next(value); } } 我不知道为什么覆盖不起作用。 可以初始化为空 最终 xpto =BehaviorSubject.seed([]);

回答 6 投票 0

为什么当没有可观察对象发出值时我的 RxJS 合并订阅会触发?

我有这样的 Angular 代码,它监听多个字段的更改,然后在其中任何一个字段发生更改时保存更改。问题是故事被保存了,尽管什么都没有……

回答 2 投票 0

我使用最新的AuthGuard方法,如果用户未登录但未能登录,它应该使用router.createUrlTree进行重定向

我无法重定向到/用户请帮忙 这是 AuthGuard,我在其中订阅了发出布尔值的 authService.authSubStatus 从“@Angular/core”导入{注入,可注入}; 重要...

回答 1 投票 0

有条件地取消()通量并保留导致取消的值

我在Java中有以下Reactive Flux流, Flux.just(e1, e2, e3, e4, e5) .flatMapSequentially(/* 处理元素 */) .scan(新累加器(null,0),(元素,总计)->新累加器(

回答 1 投票 0

无法使用主题订阅更改角度组件中的类字段

从 '@angular/core' 导入 { Component, OnInit }; 从 '../Service/authDataService' 导入 { AuthDataService }; 从“rxjs”导入{map}; 从 '../Model/userModel' 导入 { User }; 导入 {

回答 1 投票 0

Angular:ng-container 未显示

我有一个 ng-container,必须显示以防某些可观察值具有值,如果没有,则 ng-template 必须显示: 我有一个 ng-container,必须显示以防某些可观察值具有值,如果没有,则必须显示 ng-template: <ng-container *ngIf="obs$ | async; else template"> <router-outlet></router-outlet> </ng-container> <ng-template #template> <div> No data </div> </ng-template> 但是,当我在订阅后记录可观察值的值时,我发现它实际上有一个值,所以我不明白这里的问题是什么。 以下是 obs$ observable 的设置方式: obs$: Observable<Promise<SomeClass | null>> = this.obs2$.pipe( map(async (foo: SomeOtherClass | null) => { if (!foo) { return null; } const { id } = foo; const bar = await this.someService.getBarById( id ); return bar; }) ); 您需要使用 switchMap 而不是使用 map 来使用 async and await,它基本上做同样的事情。 obs$ = of(null).pipe( switchMap(() => { // replace below line with: return this.someService.getBarById(id); return of(true).pipe(delay(5000)); // simulates an API call! }) ); 完整代码: import { Component } from '@angular/core'; import { provideRouter, RouterOutlet } from '@angular/router'; import { bootstrapApplication } from '@angular/platform-browser'; import 'zone.js'; import { CommonModule } from '@angular/common'; import { ChildComponent } from './app/child/child.component'; import { map, timeout, delay } from 'rxjs/operators'; import { of, interval, switchMap } from 'rxjs'; @Component({ selector: 'app-root', standalone: true, imports: [RouterOutlet, CommonModule], template: ` <ng-container *ngIf="obs$ | async; else template"> <router-outlet></router-outlet> </ng-container> <ng-template #template> <div> No data </div> </ng-template> `, }) export class App { name = 'Angular'; obs$ = of(null).pipe( switchMap(() => { return of(true).pipe(delay(5000)); }) ); } bootstrapApplication(App, { providers: [ provideRouter([ { path: 'default', component: ChildComponent, }, { path: '**', redirectTo: 'default', pathMatch: 'full', }, ]), ], }); Stackblitz 演示

回答 1 投票 0

什么触发了combineLatest?

我有一些观察结果。我需要知道哪一个触发了订阅。 Observable.combineLatest( this.tournamentsService.getUpcoming(), this.favoriteService.getFavoriteTournam...

回答 6 投票 0

在 Angular 中将 viewChild 信号与 RxJS 一起使用时如何避免“未定义”错误?

我正在尝试使用 RxJS 中的 viewChild 信号和 fromEvent 运算符来跟踪 Angular 模板中元素上的单击事件: @成分({ 选择器:“应用程序示例”, 独立...

回答 1 投票 0

订阅 Angular 2 中的路由参数和查询参数

我已经设置了以下路由系统 导出 const MyRoutes: 路由 = [ {路径:'',redirectTo:'新',pathMatch:'完整'}, {路径:':类型',组件:MyComponent} ]; 并有以下

回答 10 投票 0

Chain API http 请求取决于先前请求的响应

我正在尝试实现链式http请求,该请求将重复X次,并在上次请求没有响应时停止。 应该如何运作。 例如我首先打电话 http://example.com/?s...

回答 3 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.