我想向我的webapi发送一个带有一些搜索参数的http.get请求,以获取学生列表。我找到了一些关于如何做到这一点的示例,但在完全按照示例中的操作进行操作后,我得到了这个奇怪的错误:
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-
}
} 导致此错误的原因可能是什么?
发布于 2017-04-06 21:43:24
看起来原生URLSearchParams被声明给您的当前代码,而new URLSearchParams();返回angular.io的URLSearchParams object
导入'rxjs/add/operator/map',它应该可以工作。
import { URLSearchParams } from '@angular/http';发布于 2017-04-06 21:27:58
尝试使用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-
}https://stackoverflow.com/questions/43256265
复制相似问题