我想将带有一些搜索参数的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-
}
}
是什么导致此错误?
最佳答案
似乎本地URLSearchParams已声明为您的当前代码,而new URLSearchParams();
返回angular.io的URLSearchParams object
导入'rxjs/add/operator/map'
,它应该可以工作。
import { URLSearchParams } from '@angular/http';
关于angular - 不能将 'URLSearchParams'类型分配给 'URLSearchParams'类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43256265/