GAPI 和 Angular 7 期
GAPI and Angular 7 issue
我请求 google 使用 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
不为空;
您已将 getFolders()
变成一个可观察对象,因此您必须订阅它才能开始从中获取数据。
this.googleDriveService.getFolders(rootFolderId).pipe(...).subscribe();
我觉得async的使用方式有问题,请试试这个:
<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>
我认为问题是由于您的异步管道,在加载异步数据之前尝试呈现内部元素。您可以做一个简单的解决方法,为您的异步数据提供一个参考变量,并在参考变量准备就绪后呈现内部内容
<ng-container *ngIf="data$ | async as data">
<mat-grid-tile *ngFor="let elem of data | async"> //note it is not data$
<app-card (input)="returnCartItem(elem)" (click)="goto(elem.header, elem.id)">
</app-card>
</mat-grid-tile>
</ng-container
问题出在 gapi (google API) 中。更多信息 here
我创建了私有方法 inObservable
private inObservable(promise) {
return from(
new Promise((resolve, reject) => {
this.zone.run(() => {
promise.then(resolve, reject);
});
})
);
}
然后把我的请求包进去
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 inObservable(promise);
我请求 google 使用 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
不为空;
您已将 getFolders()
变成一个可观察对象,因此您必须订阅它才能开始从中获取数据。
this.googleDriveService.getFolders(rootFolderId).pipe(...).subscribe();
我觉得async的使用方式有问题,请试试这个:
<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>
我认为问题是由于您的异步管道,在加载异步数据之前尝试呈现内部元素。您可以做一个简单的解决方法,为您的异步数据提供一个参考变量,并在参考变量准备就绪后呈现内部内容
<ng-container *ngIf="data$ | async as data">
<mat-grid-tile *ngFor="let elem of data | async"> //note it is not data$
<app-card (input)="returnCartItem(elem)" (click)="goto(elem.header, elem.id)">
</app-card>
</mat-grid-tile>
</ng-container
问题出在 gapi (google API) 中。更多信息 here 我创建了私有方法 inObservable
private inObservable(promise) {
return from(
new Promise((resolve, reject) => {
this.zone.run(() => {
promise.then(resolve, reject);
});
})
);
}
然后把我的请求包进去
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 inObservable(promise);