我想将带有一些搜索参数的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'.
Run Code Online (Sandbox Code Playgroud)
这是我的组件:
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-
}
}
Run Code Online (Sandbox Code Playgroud)
可能导致此错误的原因是什么?
Tha*_*ung 30
看来原生的URLSearchParams声明为你现在的代码,而new URLSearchParams();
返回angular.io的URLSearchParams 对象
导入'rxjs/add/operator/map'
它应该工作.
import { URLSearchParams } from '@angular/http';
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5183 次 |
最近记录: |