我想发送带有一些搜索参数的http.get请求到我的webapi以获取学生列表。我发现了如何做到这一点一些例子,但究竟做作为例子后,我得到这个奇怪的错误:类型'URLSearchParams'不可分配为键入'URLSearchParams'
[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(https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)。你应该使用[Angular one](https://angular.io/docs/ts/latest/api/http/index/URLSearchParams-class.html),通过从''@ angular/http''导入它,谢谢 –