Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度HttpClient post方法参数_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 角度HttpClient post方法参数

Javascript 角度HttpClient post方法参数,javascript,angular,typescript,Javascript,Angular,Typescript,我无法理解typescript中的以下类型。obj值的示例将非常有用 obj: { [name: string]: string | string[]; }; 我实际上是在试图理解Angular中HttpClient的post方法中options对象的headers属性的类型: post(url: string, body: any | null, options: { headers?: HttpHeaders | { [header: string]: strin

我无法理解typescript中的以下类型。obj值的示例将非常有用

 obj: { [name: string]: string | string[]; };
我实际上是在试图理解Angular中HttpClient的post方法中options对象的headers属性的类型:

post(url: string, body: any | null, options: {
    headers?: HttpHeaders | {
        [header: string]: string | string[];
    };
    ...
    }

您可以为headers属性的值对设置键,键将是属性名称,值当然是所述属性的值,例如:

headers: new HttpHeaders({
      'Content-Type':  'application/x-www-form-urlencoded',
      'Authorization': 'authToken'
    })
这些是随请求一起发送的标题,您可以在浏览器的开发工具的“网络”选项卡上检查这些标题

  • options
    是一个对象
  • 它有一个名为
    headers
  • 标头的类型为
    HttpHeaders
  • 其键必须符合的多个属性,在这种情况下,这些属性必须是
    字符串
  • 这些属性的值必须是
    string
    string
    s的数组

  • 刚才看到@jonrsharpe在问题下方的评论中也这么说,如果他发布了一个答案,我将删除我的答案

    在类型防御之间带有竖条的类型防御称为a,这意味着它可以是任何定义的类型

    let foo: string | number = 0; // foo must be a string or a number
    foo = "a string";
    

    返回类型可以是对象。在本例中,通过键入键的名称和类型来定义键

    const obj: {
        foo: string,
        bar: number
    } = {foo: "a string", bar: 42 }
    
    const err: {
        foo: string,
        bar: number
    } = {foo: "a string", bar: "another string" } // this would not work, because we defined bar as a number
    

    我们还使用联合类型来定义类型

    let obj: {
        foo: string,
        bar: number | string
    } = {foo: "a string", bar: "another string" } // this works because bar can be a number OR a string
    

    对象键后的问号将表明它是可选的

    const foo: {
        foo?: string
    } = {} // is valid
    
    const bar: {
        foo?: string
    } = { foo: "a string" } // is also valid
    
    const baz: {
        foo?: string
    } = {bar: "a string"} // invalid because bar is no property of type {foo?: string}
    

    {[key:string]:string}
    类型表示任何键都是允许的,只要它可以转换为字符串

    const foo: {
        [key: string]: string
    } = {foo:"a string", bar: "another string", baz: "yet another string"} // is all valid
    
    在上面编写的示例中,只要类型是string,每个键都是有效的

    const bar: {
        [key: string]: string
    } = {foo:"a string", bar: "another string", baz: 42} // is invalid baz is not of type string
    
    在此示例中,对象无效,因为baz不是
    string

    工会类型也会起作用

    const baz: {
        [key: string]: string | number
    } = {foo:"a string", bar: "another string", baz: 42} // is valid
    

    因此,根据上述规则,我们可以分解这种方法

    该方法称为post,它包含三个参数:

  • url-这是类型为
    string
    (在此方法中,它是进行POST调用的url)
  • body-这是
    any类型的
    null类型的(在此方法中,它是post调用的主体)
  • 选项:这一项看起来可能会让人困惑,因此让我们将其细分:
  • 选项需要是一个
    对象
    ,它有一个名为headers的可选键(它是optionals,因为它以问号结尾)。密钥必须命名为headers,不能命名为其他名称。无法插入其他键和值

    头文件
    必须是HttpHeaders类型(angular创建的类)一个
    对象
    ,其中键可以转换为字符串
    ,因为几乎所有东西都可以转换为字符串几乎所有东西都可以是键。值必须是字符串或字符串数组


    我希望这可以帮助您

    这是一个属性名称为字符串、属性值为字符串或字符串数组的对象:
    {foo':'bar','baz':['qux']}