gpt4 book ai didi

angular - 类型 'Headers' 与类型 'RequestOptionsArgs' 没有共同的属性?

转载 作者:太空狗 更新时间:2023-10-29 16:54:22 26 4
gpt4 key购买 nike

我刚刚对我们的 Angular 4 应用程序和构建工具进行了两项重要升级:

  1. @angular/core ^4.1.3 => ^4.2.4(和/http、/forms 等)<
  2. tslint ^5.3.2 => ^5.4.3

我有一个声明选项的服务,如下所示:

@Injectable()
export class WorkOrderService {

private headers: Headers = new Headers({ 'Content-Type': 'application/json' });
private options: RequestOptions = new RequestOptions(this.headers);

constructor(private http: Http) {}

/* Methods ... */
}

以上不再验证 tslint,抛出以下错误:

error TS2559: Type 'Headers' has no properties in common with type 'RequestOptionsArgs'.

源代码 (@angular/http interface.d.ts:43) 明确允许 Headers 作为 RequestOptionsArgs:

/**
* Interface for options to construct a RequestOptions, based on
* [RequestInit](https://fetch.spec.whatwg.org/#requestinit) from the Fetch spec.
*
* @experimental
*/
export interface RequestOptionsArgs {
url?: string | null;
method?: string | RequestMethod | null;
/** @deprecated from 4.0.0. Use params instead. */
search?: string | URLSearchParams | {
[key: string]: any | any[];
} | null;
params?: string | URLSearchParams | {
[key: string]: any | any[];
} | null;
headers?: Headers | null;
body?: any;
withCredentials?: boolean | null;
responseType?: ResponseContentType | null;
}

最佳答案

4.3 更新 HttpClient

HttpClient兼容的新语法,在 Angular 4.3 中引入,是:

import { HttpClient, HttpHeaders } from "@angular/common/http";

private _options = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) };

不再有 RequestOptions:使用新的不可变 HttpParams 添加参数 map 。

4.3 之前/Http

只是注意到RequestOptions 现在要求您将命名选项作为对象显式传递,例如:

headers: Headers = new Headers({ 'Content-Type': 'application/json' });
options: RequestOptions = new RequestOptions({ headers: this.headers });

关于angular - 类型 'Headers' 与类型 'RequestOptionsArgs' 没有共同的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44728775/

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