角度:类HttpParams上的解释参数类型[参数:字符串]:字符串|串[];

时间:2018-09-19 13:46:01

标签: angular typescript

我使用的是以下构造函数HttpParams:

constructor(options?: {
        fromString?: string | undefined;
        fromObject?: {
            [param: string]: string | string[];
        } | undefined;
        encoder?: HttpParameterCodec | undefined;
});

任何人都可以从我的平面上理解fromObject参数的含义以及如何使用它吗?

romObject?: {
     [param: string]: string | string[];
} | undefined;

1 个答案:

答案 0 :(得分:2)

它是构造函数的options参数中的可选参数。 这意味着选项(在构造函数中传递)可以具有fromObject属性(这不是强制性的)。如果存在,则必须是一个映射,其中键是字符串,值是字符串或字符串数​​组(string|string[]),也可以是undefined

因此以下内容有效

const params = new HttpParams({fromObject: {bla: 'test'}});
const params = new HttpParams({fromObject: {bla: ['test1', 'test2']}});
const params = new HttpParams({fromObject: undefined});
const params = new HttpParams({});

这无效:

const params = new HttpParams({fromObject: 'this will fail'});