【发布时间】:2018-12-15 02:31:34
【问题描述】:
我读过 in this post 我可以在 HttpParamsOptions 中使用 fromObject 将对象用作查询中的查询参数。不幸的是,我在让对象处于正确形状时遇到了一些问题。我认为它能够使用一个具有所有类型字符串的对象,并且不确定如何将其转换为正确的形状。我宁愿不必遍历对象键来让东西适合这个盒子——如果我必须这样做,它就会失去所有的价值。
谁能向我解释这里发生了什么?我已经阅读了有关可索引类型的信息,但这并没有帮助我弄清楚如何将其应用于这种情况。
报告类型错误:
TS2322: Type '{ fromObject: Query; }' is not assignable to type 'HttpParamsOptions'.
Types of property 'fromObject' are incompatible.
Type 'Query' is not assignable to type '{ [param: string]: string | string[]; }'.
Index signature is missing in type 'Query'.
调用站点(get 中以 {params: query} 开头并分解为 this):
public retrieve(query: Query): Observable<PolicyRetrieveResult> {
const q: HttpParamsOptions = {fromObject: query};
const params = new HttpParams(q);
return this.http.get<Result>(url, {params: params});
}
我的查询类:
export class Query {
id: string;
source: string = 'constant';
type: string = 'otherConstant';
}
这里是 HttpParamsOptions 供参考:
export interface HttpParamsOptions {
/**
* String representation of the HTTP params in URL-query-string format. Mutually exclusive with
* `fromObject`.
*/
fromString?: string;
/** Object map of the HTTP params. Mutally exclusive with `fromString`. */
fromObject?: {
[param: string]: string | string[];
};
/** Encoding codec used to parse and serialize the params. */
encoder?: HttpParameterCodec;
}
【问题讨论】:
-
HttpParamsOptions 不是公共角度类型。你不应该使用它。你想达到什么目的?
-
我正在努力实现这一目标:this.http.get(url, {params: query});其中 query 是我定义的一个类
标签: angular