如何在 rxjs 可观察数组中以角度添加项目

巴特尔瘤

我有一个使用 rest api 的角度服务。

@Injectable({
  providedIn: 'root'
})
export class ProductService {

  private url: string = "/app/products";

  constructor(private http: HttpClient) {
  }

  get(caregoryId: string) {
    return this.http.get<Product[]>(`${this.url}`)
      .pipe(map(products => products.filter(p => p.caregoryId == caregoryId)))
  }

  add(p: Product) {
    return this.http.post<Product>(this.url, p);
  }
}

我的组件是:

export class ProductComponent implements OnInit{

     items: Observable<Array<Product>>;

     ngOnInit() {
        this.route.params.subscribe(
          (params: any) => {
            this.items = this.productService.get(params.id);            
          })
     }

    addWidget() {
        let item: any = { name: "p-1", color: "red" } }

        this.productService.add(item))
        /// how to add created item in my items array ?

    }
}

我可以获取产品列表并使用 html 中的异步管道列出它们。但是我无法在我的可观察数组中添加创建的项目。我该怎么做?

开发商033
  1. items$您可以使用rxjs#Subject,rxjs#mergerxjs#scan控制数据流不是每次在列表中执行操作(添加、删除、更新等)时都重新分配
  2. 从 Angular 9 开始,ActivatedRoute#params弃用使用ActivatedRoute#paramMap代替;
  3. 您不必手动订阅路由参数更改,您可以使用rxjs#switchMap并执行以下操作:

this.activatedRoute.paramMap.pipe( map(paramMap => paramMap.get('id')), switchMap(id => this.productsService.getItems(id)) )

  1. 您在服务方法中有一个错字:caRegory:)

有了这些变化,我们有了:

@Component({
  // ...
})
export class AppComponent {
  readonly items$: Observable<readonly Product[]>;

  private readonly itemInsertedSource$ = new Subject<Product>();
  private readonly itemInserted$ = this.itemInsertedSource$.asObservable();

  constructor(
    private readonly activatedRoute: ActivatedRoute, 
    private readonly productsService: ProductsService
  ) {      
    this.items$ = merge(          
      this.activatedRoute.paramMap.pipe(
        map(paramMap => paramMap.get('id')),
        switchMap(id => this.productsService.getItems(id))
      ),
      this.itemInserted$
    ).pipe(scan((acc, value) => [...acc, value]));
  }

  addWidget(): void {
    const item: Product = { color: 'purple', name: 'p-3' };

    this.productsService.add(item).subscribe(itemInserted => this.itemInsertedSource$.next(itemInserted));   
  }
}

STACKBLITZ 演示

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何触发可观察的rxjs

如何链接可观察的rxjs

rxjs如何在角度服务中一起返回可观察到的值

如何解析rxjs中可观察对象数组中的html元素

RxJS-如何对异步可观察对象数组使用toArray()?

Angular 11 RxJS:如何将对象添加到可观察对象的嵌套列表中?

如何从可观察的 rxjs 中获取两个或多个值作为数组?

如何将数组的可观察对象转换为可观察对象数组(RxJS)

如何在RXJS中使用可观察变量来转换数组?

稍后如何在 rxjs 可观察流中使用属性?

如何在RxJS(或Reactive Extensions中的常规)中实现可观察到的时间到期热

如何“等待” RxJS中的两个可观察对象

如何使用RxJS可观察的订阅调用中的值

如何停止Rxjs中可观察到的共享

如何在Rxjs中缓冲一个简单的可观察对象?

如何在RxJs 5中将主题转换为可观察对象

如何在订阅期间从可观察的Angular 5 RxJS中检查NULL结果?

如何在 rxjs 中组合一系列可观察的

如何在Angular / RxJS中合并两个可观察对象?

如何在rxjs 5中测试使用定时间隔返回可观察值的函数?

RxJS可观察的合并角度

如何将可观察结果链接到rxjs中的以下可观察结果

如何等待可观察但在rxjs中返回先前的可观察结果?

如何基于rxjs中的现有可观察者创建新的可观察订阅?

Knockoutjs:如何为可观察数组中的每个项目添加一个额外的字段

通过添加,过滤或删除项目来更新可观察到的Rxjs数组

如何退订可观察的RxJS 5?

如何链接可观察的RxJS列表

RxJs:如何根据可观察状态循环?