【问题标题】:@angular/common/http has no exported member 'RequestOptions'@angular/common/http 没有导出成员 'RequestOptions'
【发布时间】:2019-05-27 13:41:54
【问题描述】:

我想使用 firebase 函数对 angular 6 的电子邮件进行打磨,但 angular 会抛出此错误:

错误 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) })
    }
}

【问题讨论】:

标签: angular angular-httpclient


【解决方案1】:

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)

【讨论】:

    猜你喜欢
    • 2019-01-17
    • 1970-01-01
    • 2017-07-05
    • 2017-07-07
    • 2022-12-20
    • 1970-01-01
    • 2019-04-06
    • 2018-09-25
    • 2018-05-22
    相关资源
    最近更新 更多