【发布时间】:2019-09-11 05:57:28
【问题描述】:
我请求使用 gapi 谷歌驱动器:
getFolders(folderId: string): Observable<{ id: string, name: string }[]> {
const promise = gapi.client.drive.files.list({
fields: 'incompleteSearch,nextPageToken,files(id,name)',
q: `'${folderId}' in parents`,
}).then((res) => {
return JSON.parse(res.result.files);
});
return from(promise);
}
然后我尝试在组件中显示此数据:
.ts 文件ngOnInit:
this.data$ = this.googleDriveService.getFolders(rootFolderId)
.pipe(
map((files) => {
debugger;
return files.map(file => ({ id: file.id, header: file.name, content: '', imageUrl: this.defaultImageUrl }));
}),
takeUntil(this.destroy$),
);
和html文件:
<mat-grid-tile *ngFor="let elem of (data$ | async)">
<app-card (input)="returnCartItem(elem)" (click)="goto(elem.header, elem.id)"></app-card>
</mat-grid-tile>
问题是data$ 总是空的。我将debugger 添加到map 以检查那里可能有问题,但它永远不会进入map 函数。从响应中,我得到 2 个文件,所以 res.result.files 不为空;
【问题讨论】:
-
您的意思是在
'${folderId}'周围加上单引号吗? -
@AryanJ-NYC,不幸的是没有,问题是我发送请求,得到响应(非空),但由于
data$- 空 -
当您使用
async pipe时,您不需要takeUntil(this.destroy$),因为async pipe会为您取消订阅。 -
@fridoo thx 获取信息
标签: angular typescript rxjs rxjs6 rxjs-pipeable-operators