错误TS2559:“标头"类型与"RequestOptionsArgs"类型没有共同的属性 [英] error TS2559: Type 'Headers' has no properties in common with type 'RequestOptionsArgs'

查看:600
本文介绍了错误TS2559:“标头"类型与"RequestOptionsArgs"类型没有共同的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,但是得到相同的结果.我该如何解决才能使其正常工作?

This is my code but, I'm getting the same result. how can I fix this for it to work?

错误TS2559:类型'Headers'与类型'{headers没有共同的属性:HttpHeaders | {[header:string]:字符串|细绳[]; };观察?参数?:Ht ...'.

error TS2559: Type 'Headers' has no properties in common with type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'.

import { Component, OnInit } from '@angular/core';
import { Http, Headers, Response, URLSearchParams } from '@angular/http';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import 'rxjs/add/operator/toPromise';

@Component({
  selector: 'app-send-mail',
  templateUrl: './send-mail.component.html',
  styleUrls: ['./send-mail.component.css']
})
export class SendMailComponent implements OnInit {

  constructor(private http:  HttpClient) { }

  sendEmail() {

    const url = `https://your-cloud-function-url/function`;
    const params: URLSearchParams = new URLSearchParams();
    const headers = new Headers({'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' });

    params.set('to', 'email');
    params.set('from', 'email@website.com');
    params.set('subject', 'test-email');
    params.set('content', 'Hello World');

    return this.http.post(url, params, headers)
                    .toPromise()
                    .then( res => {
                      console.log(res);
                    })
                    .catch(err => {
                      console.log(err);
                    });

  }

推荐答案

由于您正在使用'@angular/common/http';中的新HttpClient(在Angular 4中引入)以及HttpClientModule来进行POST调用,因此您应该使用 HttpHeaders而不是'@angular/http';中的Headers.您还需要将标头作为{headers}之类的对象传递.

As you are using the new HttpClientfrom '@angular/common/http'; (introduced in Angular 4) along with the HttpClientModule to make the POST call, hence you should use HttpHeaders instead of Headers from '@angular/http'; . You also need to pass the the headers as an Object like {headers}.

这是修改后的代码-

sendEmail() {

    const url = `https://your-cloud-function-url/function`;
    const params: URLSearchParams = new URLSearchParams();
    const headers = new HttpHeaders({'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' });

    params.set('to', 'email');
    params.set('from', 'email@website.com');
    params.set('subject', 'test-email');
    params.set('content', 'Hello World');

    return this.http.post(url, params, {headers})
                    .toPromise()
                    .then( res => {
                      console.log(res);
                    })
                    .catch(err => {
                      console.log(err);
                    });

  }

这篇关于错误TS2559:“标头"类型与"RequestOptionsArgs"类型没有共同的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆