如何 return promise 函数中的 Observable
How to return a Observable in promise function
我用 ionic + capacitor + angular 写了一个 http 拦截器。因为我使用的是电容器,所以我正在访问 promise 函数中的存储。但是 http 拦截器将我们排除在 return HttpEvent Observable 之外。这是我的代码
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpHandler, HttpRequest, HttpEvent, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Plugins } from '@capacitor/core';
const { Storage } = Plugins;
@Injectable()
export class Interceptor implements HttpInterceptor {
constructor() { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.getItem().then(value => {
const token = value['value'];
if (token) {
request = request.clone({ headers: request.headers.set('Authorization', 'Bearer ' + token) });
}
if (!request.headers.has('Content-Type')) {
request = request.clone({ headers: request.headers.set('Content-Type', 'application/json') });
}
request = request.clone({ headers: request.headers.set('Accept', 'application/json') });
return next.handle(request).pipe(
map((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
}
return event;
}));
});
}
async getItem() {
return await Storage.get({ key: 'revolt-token' });
}
}
到目前为止我已经试过了
return from(this.getItem()).pipe(map(data => {
return next.handle(request).pipe(
map((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
}
return event;
}));
});
但是 returns Observable<Observable<HttpEvent<any>>>
但我想要 Observable<HttpEvent<any>>
谁能帮我确定如何从 Promise 中 return Observable。谢谢。
答案是pipe switchMap而不是map
在 return from(this.getItem()).pipe(map(data => {}));
我用 ionic + capacitor + angular 写了一个 http 拦截器。因为我使用的是电容器,所以我正在访问 promise 函数中的存储。但是 http 拦截器将我们排除在 return HttpEvent Observable 之外。这是我的代码
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpHandler, HttpRequest, HttpEvent, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Plugins } from '@capacitor/core';
const { Storage } = Plugins;
@Injectable()
export class Interceptor implements HttpInterceptor {
constructor() { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.getItem().then(value => {
const token = value['value'];
if (token) {
request = request.clone({ headers: request.headers.set('Authorization', 'Bearer ' + token) });
}
if (!request.headers.has('Content-Type')) {
request = request.clone({ headers: request.headers.set('Content-Type', 'application/json') });
}
request = request.clone({ headers: request.headers.set('Accept', 'application/json') });
return next.handle(request).pipe(
map((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
}
return event;
}));
});
}
async getItem() {
return await Storage.get({ key: 'revolt-token' });
}
}
到目前为止我已经试过了
return from(this.getItem()).pipe(map(data => {
return next.handle(request).pipe(
map((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
}
return event;
}));
});
但是 returns Observable<Observable<HttpEvent<any>>>
但我想要 Observable<HttpEvent<any>>
谁能帮我确定如何从 Promise 中 return Observable。谢谢。
答案是pipe switchMap而不是map 在 return from(this.getItem()).pipe(map(data => {}));