我想将带有一些搜索参数的http.get请求发送到我的webapi以获取学生列表。我找到了一些关于如何执行此操作的示例,但在完成示例之后,我得到了这个奇怪的错误:
[ts]
Type 'URLSearchParams' is not assignable to type 'URLSearchParams'. Two different types with this name exist, but they are unrelated.
Property 'rawParams' is missing in type 'URLSearchParams'.
这是我的组成部分:
import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions } from '@angular/http';
import 'rxjs/add/operator/map'
import { User } from '../_models/user';
@Injectable()
export class UserService {
options = new RequestOptions({ 'headers': new Headers({ 'Content-Type': 'application/json' })});
constructor(private http: Http) {
}
createAccount(newUser: User){
return this.http.post('http://localhost:64792/api/students', JSON.stringify(newUser), this.options)
.map((response: Response) => {
if(response.ok){
console.log("Registration was a success");
return true;
} else {
console.log("Registration failed");
return false;
}
});
}
searchStudents(searchWords: Array<string>){
// Parameters obj-
let params: URLSearchParams = new URLSearchParams();
for(let i = 0; i < searchWords.length; i++){
params.append('searchWords', searchWords[i]);
}
this.options.search = params;
//Http request-
}
}
可能导致此错误的原因是什么?
答案 0 :(得分:30)
似乎原始URLSearchParams已声明为您当前的代码,而new URLSearchParams();
则返回angular.io&#39; s URLSearchParams object
导入'rxjs/add/operator/map'
,它应该有用。
import { URLSearchParams } from '@angular/http';
答案 1 :(得分:1)
尝试使用set方法
searchStudents(searchWords: Array<string>){
// Parameters obj-
let params: URLSearchParams = new URLSearchParams();
for(let i = 0; i < searchWords.length; i++){
params.set('searchWords', searchWords[i]);
}
this.options.search = params;
//Http request-
}