angular - “HttpParams”类型的参数不能分配给“URLSearchParams”类型的参数
问题描述
我正在更新一个应用程序以使用来自 Angular 4 的 Angular 8。我以前从未真正使用过 Angular,并且我遇到了几个弹出错误。我有这个错误出现:
Argument of type 'HttpParams' is not assignable to parameter of type 'URLSearchParams'.
当试图运行这个
queryBatches(searchParams: HttpParams): Promise<PaginatedData> {
return this.authService.generateAuthHeader({'Content-Type': 'application/json'}, searchParams).then(options => {
return this.http.get(endpoints.query.batches({}), options)
.map(response => {
return new PaginatedData(response.json(), Batch);
}).toPromise();
});
}
这是我要导入的
import {Injectable} from '@angular/core';
import {PaginatedData} from '../../models/common';
import {Batch} from '../../models/batch/batch.model';
import {AuthService} from '../auth/auth.service';
import endpoints from '../endpoints';
import {HttpParams} from '@angular/common/http'
import {Http, URLSearchParams} from '@angular/http';
import {BatchMismatch} from '../../models/batch/batch-mismatch-model';
import {BatchMismatchReviewer} from '../../models/batch/batch-mismatch-reviewer-model';
import {BatchMisMatchReload} from '../../models/batch/batch-mismatch-reload-model';
对此问题的任何帮助将不胜感激
编辑:我还应该以我最初没有编写此代码作为开头。
生成AuthHeader:
generateAuthHeader(additionalHeaders?: {[index: string]: any}, searchParams?: URLSearchParams): Promise<RequestOptions> {
if (this.cookieService.get('auth-cookie')) {
let authCookie = JSON.parse(this.cookieService.get('auth-cookie')) as AuthCookie;
const headers = new Headers({'Authorization': `Bearer ${authCookie.token}`});
if (additionalHeaders) {
for ( let headerValue of Object.keys(additionalHeaders)) {
headers.append(headerValue, additionalHeaders[headerValue]);
}
}
const options = new RequestOptions({headers: headers, params: searchParams});
return Promise.resolve(options);
}
else {
return Promise.reject({status: 401});
}
}
解决方案
错误正是它所说的。
generateAuthHeader
的第二个参数应该是 type 的URLSearchParams
,但是你传递给它一个 type 的对象HttpParams
。
现在,无论它是否真的是那种类型,你都需要控制台日志来查看参数。如果只是输入错误,请更新类型以使其匹配。如果queryBatches
确实收到了一个HttpParams
对象,那么您将需要弄清楚如何制作一个新对象以传递给generateAuthHeader
推荐阅读
- postgresql - 从非备份文件“挂载”一个 PostgreSQL 数据库
- python - django-cms: ImportError: No module named 'cms.models'
- codeigniter - crontab 中的 Codeigniter 网址
- node.js - 如何在node.js中传递`hgetall`中的值
- php - 如果 ACF 中继器子字段为空,则隐藏 div 容器
- html - 如何在标签中设置图像样式?
- node.js - 快速会话:在选项卡/浏览器关闭时终止会话
- python - 使用 PANDAS 将两列合并并操作为日期
- docker - 安装证书后的 SSL_ERROR_BAD_CERT_DOMAIN
- python - 为什么 ScrollView 不能正常工作?