我正在从事angular 6项目,我需要在多个组件中调用get API服务,因此什么是最好的解决方案,以避免在不同组件中多次调用同一API,并且只能在所需组件中访问数据,请提供示例谢谢
答案 0 :(得分:1)
您可以使用ngrx/store存储从api调用接收的数据,并将其用于不同的组件。
答案 1 :(得分:0)
您可以利用HTTP_INTERCEPTORS和缓存机制来避免多次API调用。
import { Injectable } from '@angular/core';
import { HttpEvent, HttpRequest, HttpResponse, HttpInterceptor, HttpHandler } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { startWith, tap } from 'rxjs/operators';
import 'rxjs/add/observable/of';
import { RequestCache } from './request-cache.service';
@Injectable()
export class CachingInterceptor implements HttpInterceptor {
constructor(private cache: RequestCache) {}
intercept(req: HttpRequest<any>, next: HttpHandler) {
const cachedResponse = this.cache.get(req);
return cachedResponse ? Observable.of(cachedResponse) : this.sendRequest(req, next, this.cache);
}
sendRequest(
req: HttpRequest<any>,
next: HttpHandler,
cache: RequestCache): Observable<HttpEvent<any>> {
return next.handle(req).pipe(
tap(event => {
if (event instanceof HttpResponse) {
cache.put(req, event);
}
})
);
}
}
import { Injectable } from '@angular/core';
import { HttpRequest, HttpResponse } from '@angular/common/http';
const maxAge = 30000;
@Injectable()
export class RequestCache {
cache = new Map();
get(req: HttpRequest<any>): HttpResponse<any> | undefined {
const url = req.urlWithParams;
const cached = this.cache.get(url);
if (!cached) {
return undefined;
}
const isExpired = cached.lastRead < (Date.now() - maxAge);
const expired = isExpired ? 'expired ' : '';
return cached.response;
}
put(req: HttpRequest<any>, response: HttpResponse<any>): void {
const url = req.url;
const entry = { url, response, lastRead: Date.now() };
this.cache.set(url, entry);
const expired = Date.now() - maxAge;
this.cache.forEach(expiredEntry => {
if (expiredEntry.lastRead < expired) {
this.cache.delete(expiredEntry.url);
}
});
}
}
app.module.ts 中的import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { RequestCache } from './request-cache.service';
import { CachingInterceptor } from './caching-interceptor.service';
// ...
providers: [
RequestCache,
{ provide: HTTP_INTERCEPTORS, useClass: CachingInterceptor, multi: true }
],
// ...
答案 2 :(得分:0)
您可以使用rxjs shareReplay。像
let data$ = this.http.get(...url...).pipe(
shareReplay()
);
现在,每个可观察数据的订户将通过一次http调用获取数据
let call1 = data$.subsribe();
let call2 = data$.subscribe();