gpt4 book ai didi

javascript - 如何在 Angular 8 中创建通用请求服务

转载 作者:行者123 更新时间:2023-12-03 07:05:13 25 4
gpt4 key购买 nike

我已经创建了一个通用请求服务,但它一直返回“ZoneAwarePromise”。我尝试使用 .pipe() 和 .subscribe() 来尝试检索请求的内容,但它不起作用。

请求者.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
providedIn: 'root'
})
export class RequesterService {

constructor(private http: HttpClient) { }

request(method, url, headers, body, params) {
return this.http.request(method, url, {
body: body || {},
headers: headers || {},
params: params || {}
})
}

async get(url, params?, headers?, data?) {
return await this.request('get', url, params, headers, data)
}

async post(url, params, headers, data) {
return await this.request('post', url, params, headers, data);
}

async put(url, params, headers, data) {
return await this.request('put', url, params, headers, data);
}

async delete(url, params, headers, data) {
return await this.request('delete', url, params, headers, data);
}
}

gym-list.component.ts

import { Component, OnInit } from '@angular/core';
import { RequesterService } from 'src/app/core/Requester/requester.service';

@Component({
selector: 'app-gym-list',
templateUrl: './gym-list.component.html',
styleUrls: ['./gym-list.component.css']
})
export class GymListComponent implements OnInit {

constructor(private Requester: RequesterService) { }

ngOnInit() {
console.log(this.teste())
}

async teste() {
await this.Requester.get('https://searchs.glitch.me/proposalcontent')
}
}

最佳答案

import { HttpClient, HttpHeaders, HttpRequest, HttpEventType, HttpEvent, HttpResponse, HttpErrorResponse } from "@angular/common/http";
import { Observable, from } from 'rxjs';
import { IGenericHttpClient } from './igeneric-http-client';
import { Injectable } from '@angular/core';
import { HttpMethod } from '../view-models/component-type.enum'
import { IRequestOptions } from '../view-models/interface';
import { EnvironmentViewModel } from '../view-models/environment-view-model';
import { retry } from 'rxjs/operators';
@Injectable()
export class GenericHttpClient<T> implements IGenericHttpClient<T>{

constructor(private httpClient: HttpClient, private environment: EnvironmentViewModel) { }

public Post(destinationUrl: string, options?: IRequestOptions): Observable<T> {
return this.request<T>(HttpMethod.Post, destinationUrl, options);
}

public Put(destinationUrl: string, options?: IRequestOptions): Observable<T> {
return this.request<T>(HttpMethod.Put, destinationUrl, options);
}

public Get(destinationUrl: string, options?: IRequestOptions): Observable<T> {
return this.request<T>(HttpMethod.Get, destinationUrl, options);
}

public Delete(destinationUrl: string, options?: IRequestOptions): Observable<T> {
return this.request<T>(HttpMethod.Delete, destinationUrl, options);
}

private request<T>(method: string, url: string, options?: IRequestOptions): Observable<T> {
return Observable.create((observer: any) => {
this.httpClient.request<T>(new HttpRequest(method, this.environment.baseUrl + url, options)).subscribe(
(response: any) => {
const responsTye = response as HttpEvent<any>
switch (responsTye.type) {
case HttpEventType.Sent:
console.log('Request sent!');
break;
case HttpEventType.ResponseHeader:
console.log('Response header received!');
break;
case HttpEventType.DownloadProgress:
const kbLoaded = Math.round(responsTye.loaded / 1024);
console.log(`Download in progress! ${kbLoaded}Kb loaded`);
break;
case HttpEventType.Response:
observer.next(response.body);
console.log('😺 Done!', responsTye.body);
}
},
(error) => {
switch (error.status) {
case 403:
observer.complete();
break;
default:
observer.error(error);
break;
}
}
);
});
}
}

关于javascript - 如何在 Angular 8 中创建通用请求服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59472903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com