javascript - 没有导出的成员 '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]
解决方案
您从错误的包中导入了它。另外,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 }
(如果它们相同,则不需要定义键和值)
希望能帮助到你 :)
推荐阅读
- reactjs - 如何使用 google places api 获取城市照片
- r - R:计算多变量相关矩阵时如何实现并行处理?
- python - 每 40 个字符插入一个换行符不起作用(但没有错误)
- random - Puppeteer 随机用户代理参数
- python - if 语句为字符串变量创建新列 pandas
- android - 是否可以让 Laravel Sanctum 同时使用令牌和身份验证?
- r - R中必要时矩阵未填充1
- ruby-on-rails - rails file_field 未显示
- jsp - Servlet 在响应上设置的 Cookie 在转发的 JSP 的请求中不可用
- apache - 在我的 Apache Docker 容器中,如何设置代理来路由其他代理未处理的所有内容?