@ angular / common / http没有导出的成员'RequestOptions'

时间:2018-12-30 18:43:27

标签: angular angular-httpclient

我想使用Firebase函数对角度为6的电子邮件进行打磨,但是角度会引发此错误:

  

错误TS2305:模块'“ E:/ project / node_modules / @ angular / common / http”'   没有导出的成员“ RequestOptions”。

import { Injectable } from '@angular/core';
import { NgForm } from '@angular/forms';
import { HttpClient, HttpHeaders, RequestOptions } from '@angular/common/http';
import 'rxjs';

@Injectable()
export class MailerService {

    constructor(private httpClient: HttpClient) { }

    send(form: NgForm) {

        let url = 'TheUrlOfMyFunction';
        let params: URLSearchParams = new URLSearchParams();
        let options = {headers: new HttpHeaders({'Content-Type':  'application/json'})};

        params.set('to', form.value['emailTo']);
        params.set('from', form.value['emailFrom']);
        params.set('subject', form.value['object']);
        params.set('content', form.value['text']);

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

    }
}

1 个答案:

答案 0 :(得分:3)

RequestOptions在“ @ angular / common / http”模块中不可用。它以前在“ @ angular / http”模块中可用,现在已在最新的angular版本中弃用。您现在可以像这样创建本地变量-

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

您可以在进行http调用时使用httpOptions。

return this.http.get('PRODUCT_URL', httpOptions)