首页 > 解决方案 > 没有导出的成员 'Headers' 和 RequestOptions

问题描述

https://www.techiediaries.com/ionic-http-post/

我正在关注本教程。运行 ionic serve 时出现以下错误

ng] ERROR in src/app/home/home.page.ts:2:22 - error TS2305: Module '"../../../node_modules/@angular/common/http"' has no exported member 'Headers'.
[ng] 2 import { HttpClient, Headers, RequestOptions } from '@angular/common/http';
[ng]                        ~~~~~~~
[ng] src/app/home/home.page.ts:2:31 - error TS2305: Module '"../../../node_modules/@angular/common/http"' has no exported member 'RequestOptions'.
[ng] 2 import { HttpClient, Headers, RequestOptions } from '@angular/common/http';
[ng]                                 ~~~~~~~~~~~~~~
[ng] src/app/home/home.page.ts:28:11 - error TS2339: Property 'http' does not exist on type 'HomePage'.
[ng] 28      this.http.post("http://localhost/android/Api.php?apicall=signup", postData, requestOptions)
[ng]              ~~~~
[ng] 

标签: javascriptnode.jsangularionic4

解决方案


您从错误的包中导入了它。另外,Headers已重命名为HttpHeaders.

尝试以下操作:

import { RequestOptions } from 'http';
import { HttpHeaders } from '@angular/common/http';

此外,您不一定需要RequestOptions. 您也可以HttpHeaders像这样直接传递:

let headers: HttpHeaders = req.headers;

if (this.authService.checkAuth()) {
    const token = 'something';
    headers = headers.set('Authorization', `Bearer ${token}`);
}

const authReq = req.clone({ headers });

我也看到它this.http不存在,所以你需要像这样将它注入你的constructor()

constructor(private http: HttpClient) {}

在您的情况下,您可以像这样发送您的请求:

this.http.post("http://localhost/android/Api.php?apicall=signup", postData, { headers });

{ headers }你的对象在哪里RequestOptions,是简写{ headers: headers }

(如果它们相同,则不需要定义键和值)

希望能帮助到你 :)


推荐阅读