角度 12 错误作为类型 '(response: IAppConfig)=>void' 的参数不可分配给类型 '(value:Object)=>void|PromiseLike<



Angular 12错误:(response: IAppConfig)=>void类型的参数不能赋值给(value:Object)=>void|PromiseLike<void>类型的参数

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { IAppConfig } from './app-config.model';
@Injectable()
export class AppConfig {
static settings: IAppConfig;
constructor(private http: HttpClient) {}
load() {
const jsonFile = 'assets/config/config.json';
return new Promise<void>((resolve, reject) =>
this.http.get(jsonFile).toPromise().then((response : IAppConfig) => {
AppConfig.settings = <IAppConfig>response;
resolve();
}).catch((response: any) => {
console.log('Configuration file could not be read');
resolve();
});
});  
}
}

您可以通过删除响应类型来修复它:

this.http.get(jsonFile).toPromise().then(response => {
AppConfig.settings = <IAppConfig>response;
resolve();
}

请注意,toPromise在RxJS 7中已被弃用,并将在RxJS 8中删除。所以这里应该使用lastValueFrom:

import { lastValueFrom } from 'rxjs';
...
return new Promise<void>((resolve, reject) => {
const configSetting = this.http.get(jsonFile);
lastValueFrom(configSetting).then((response: IAppConfig) => {
AppConfig.settings = <IAppConfig>response;
resolve();
})
...

相关内容

最新更新