angular - Angular 8:在不订阅 observable 的情况下获取值
问题描述
以下是我需要在应用程序的各个部分订阅的两种方法:
getProducts(): Observable<Product[]> {
return this.http.get<Product[]>(this.url);
}
getProductsByIds(ids: number[]): Observable<Products[]> {
return this.getProducts()
.map(products => products.filter(products => ids.includes(product.id)));
}
这适用于我的大多数组件,但在一个组件中,我id
事先知道 -s 并且只想显示 id-s 1、2、9、18 的 4 个产品。我想像这样调用方法:) getProductsByIds([1, 2, 9, 18])
。我只知道如何订阅这两个 observables,但我怎样才能一劳永逸地获得这 4 个产品(不需要值流)?我一定错过了一些基本的讲座..
解决方案
如果您只想获取一些值并将它们粘贴到没有 Observable 的变量中。从您的组件中,您可以像这样调用 getProductByIds :
service.getProductByIds([1,2,9.8]).take(1).subscribe(x => {
this.variableWithProducts = x;
});
推荐阅读
- javascript - 如何在角度 TypeScript 方法中将 Map 定义为参数?
- python - 手动重新渲染时获取复选框选项名称
- cmake - find_package 后在我的项目中找不到好的包含目录和库
- java - 如何在字符串中切换字母
- html - 如何将此项目转换为代码?
- c# - 使用正则表达式编辑具有文本和多个图像的 html 字符串
- c# - 301 使用 AllowAutoRedirect 永久移动
- r - 将数据框转换为R中的列表列表
- docker - mac k8s模式下docker的linux版本是多少?
- graphql - graphcool 中的一个关系使部署无缘无故失败