- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在调用 spotify api,它返回一个像这样的对象:
{
next: 'https://api.spotify.com/v1/me/tracks?offset=100&limit=50'
items: [...]
}
其中 items 是刚刚进行的网络调用的结果,next 是用于获取下“页面”结果的 url。我想做的是在没有偏移的情况下进行初始调用,然后继续进行网络调用直到 next 为空,这意味着用户没有更多的项目可以获取。如果没有更多项目,他们会为下一个返回 null。
这似乎是可能的,但似乎无法弄清楚如何正确地做到这一点。我正在寻找的是这样的东西:
readonly baseSpotifyUrl: string = 'https://api.spotify.com/v1';
constructor(private http: HttpClient) {}
public getTracks(url?: string): Observable<any> {
return this.http.get(url && url?.length > 0 ? url : `${this.baseSpotifyUrl}/me/tracks?limit=50`);
}
public getAllTracks(): Observable<any> {
const tracks$: Subject<any> = new Subject();
let next: string = '';
this.getTracks(next)
.subscribe({
next: (res: any): void => {
tracks$.next(res.items);
next = res.next;
// if res.next !== null, do this again now that we have set next to res.next
},
error: (err: any): void => {
console.error(err);
}
});
return tracks$;
}
这里的想法是,我的组件将调用 getAllTracks() 并接收一个主题,然后新项目将不断推送到该主题,直到检索到所有项目。我似乎无法弄清楚如何在前一个返回时发出新的网络请求只有在有更多项目要获取时才返回(res.next!== null)
编辑-------------------------------------------- --------------
这完成了工作,但我觉得它很垃圾:
public getAllTracksSegment(itemsSubject: Subject<any>, nextSubject: Subject<string>, url?: string): void {
this.http.get(url && url?.length > 0 ? url : `${this.baseSpotifyUrl}/me/tracks?limit=50`).subscribe({
next: (res: any): void => {
itemsSubject.next(res.items);
nextSubject.next(res.next);
}
});
}
public getAllTracks(): Observable<any> {
const tracks$: Subject<any> = new Subject();
const next$: Subject<string> = new Subject();
next$.subscribe({
next: (next: any): void => {
if (next !== null) {
this.getAllTracksSegment(tracks$, next$, next);
}
}
});
next$.next('');
return tracks$;
}
最佳答案
如果我对问题的理解正确,我会使用 expand
运算符来构建解决方案。
这是我要使用的代码。评论是内联的
public getTracks(url?: string): Observable<any> {
return this.http.get(url && url?.length > 0 ? url : `${this.baseSpotifyUrl}/me/tracks?limit=50`);
}
public getAllTracks(): Observable<any[]> {
// the first call is with no parameter so that the default url with no offset is used
return getTracks().pipe(
// expand is used to call recursively getTracks until next is null
expand(data => data.next === null ? EMPTY : getTracks(data.next)),
// with tap you can see the result returned by each call
tap(data => console.log(data)),
// if you want you can use the reduce operator to eventually emit the
// accumulated array with all items
reduce((acc, val) => {
acc = [...acc, ...val.items]
return acc
}, [])
)
}
// now you can fire the execution of the recursive calls by subscribing
// to the observable returned by getAllTracks
getAllTracks().subscribe(
// allItems is an array containing all the items returned by the various calls
allItems => console.log(allItems)
)
@skyleguy 评论后的补充说明
tap
运算符用于实现副作用。换句话说,它从上游接收所有通知,对通知的数据做任何它需要做的事情,然后将相同的通知传递给下游。传递给 tap
操作符的函数不需要返回任何东西。在应用副作用 之后,上游只是传递给下游。在此示例中,副作用只是在控制台上打印随通知传递的数据。
在 pipe
中使用的 reduce
是 RxJs 的 reduce
运算符,而不是 reduce
方法 数组
。 reduce
RxJs 运算符累积从上游通知的所有数据,并在上游完成
时发出仅一个值。因此,在这个例子中,每次调用远程函数都会返回一些东西,这个东西会进入 reduce
运算符并参与累加逻辑。当 expand
返回 EMPTY
Observable 时,在递归结束时,EMPTY
Observable 只是完成
而不通知任何东西,这意味着上游 completes
因此 reduce
可以发出它的第一个也是唯一的通知,即包含所有项目的数组,然后 complete
.
This stackblitz通过模拟远程调用来复制此逻辑。
关于angular - 使用 RXJS 继续以 Angular 触发 http 调用,直到满足条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68686727/
我正在尝试在项目中学习和添加 Angular 国际化。我只能理解 Angular 文档 (https://angular.io/guide/i18n-overview) 的编译时翻译。 我需要这样的东
在我的 Angular 应用程序中,基于登录用户,我想通过显示/隐藏不同的菜单项或允许/禁止某些路由来授予或限制功能。 目前成功登录后,我的 .NET Core API 会返回一个 JWT token
我是 Angular 的新手,目前我已经看过 angular.io 网站提供的一些示例。但是在component decorator在文档中的解释,它指出 Angular components are
这里是service employee-service.service.ts的代码 import { Injectable } from '@angular/core'; import { HttpC
我目前正在使用@angular/http URLSearchParams 类来检索 URL 参数。在 Angular 5 中,注意到这已被弃用,但我没有看到以我当前使用的方式替换 URLSearchP
我目前正在使用@angular/http URLSearchParams 类来检索 URL 参数。在 Angular 5 中,注意到这已被弃用,但我没有看到以我当前使用的方式替换 URLSearchP
如何正确安装 PUG/JADE 到 Angular 2 或更高版本 这样在工作和 AOT 和 JiT 的同时 工作单元和集成测试 并且在创建每个新组件时不会受到太多影响 最佳答案 我看到了很多解决方案
我的 Angular 12 应用程序中有一些通用组件,我计划将其创建为一个 Angular 库,以便其他应用程序也可以使用它。我们有一些应用程序在较低版本的 angular(例如 angular 8/
tl;dr; ng build 删除了包含我编译的自定义库的/dist 文件夹。这会使我项目代码中对该库的所有引用无效,从而导致 ng build 最终失败。我做错了什么? 我关注了documenta
我正在将一些“遗留”(非 typescript )js 库导入到我的 Angular SPA 中。 通常我只是从 cdn 添加一个负载到 index.html 就像: 在 Angular 分量中我只
我有这个 angular 应用程序,它基本上使用了库的概念。 我有 2 个名为 的库Lib1 和 lib2 根据他们所服务的微服务分组。 现在我将这些库导入主应用程序,即 应用1 事情一直到现在。 现
我在我的项目中启用了 angular Universal。我现在想完全删除它。我试图删除以下文件 /server.ts /webpack.server.config.js /src/tsconfig.
我已经有一个 AuthService 在登录时对用户进行身份验证,并且 AuthGuard 在未登录的情况下阻止访问。 某些页面我通过 UserProfile/Role 限制访问,但现在我需要阻止页面
我正在尝试使用 angular、TypeORM、SQLite 和其他组件作为 webpack 构建 Electron 应用程序。 我从在 GitHub 上找到的示例开始我的开发:https://git
我在从 Angular 8 更新到 9 并运行时遇到以下错误 ng 更新@angular/material: Package "@angular/flex-layout" has an incompa
我正在尝试使用 Angular 9,我想创建一个项目,然后创建一个库项目并开始向其中添加我想稍后在 GitHub 上发布的通用模块,并在我的本地使用这些库项目。 相关依赖如下: Angular CLI
我正在尝试使用 Angular 9,我想创建一个项目,然后创建一个库项目并开始向其中添加我想稍后在 GitHub 上发布的通用模块,并在我的本地使用这些库项目。 相关依赖如下: Angular CLI
我正在我的 h1 元素“之前”创建一个小的程式化三 Angular 形图案,但我无法正确地圆 Angular 。右上角没问题,但其他两个有剪裁问题。 这是输出以及形状的放大图像: 使用的代码如下: h
我有一个 Angular 元素,带有自定义标记名 - fancy-button。如何将 fancy-button 嵌入 Angular 应用程序? 我已经尝试了以下方法,但都没有用 - 在 index
我已将我的项目从 angular 5.2.9 升级到 angular 6.0.0-rc.5。 除了包路径中的几个快速 RxJS 修复外,一切看起来都不错。(此链接非常有用:Want to upgrad
我是一名优秀的程序员,十分优秀!