gpt4 book ai didi

angular - 类型 'URLSearchParams' 不可分配给类型 'URLSearchParams'

转载 作者:太空狗 更新时间:2023-10-29 16:53:48 27 4
gpt4 key购买 nike

我想向我的 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-
}
}

什么可能导致此错误?

最佳答案

好像是原生URLSearchParams声明为您当前的代码,而 new URLSearchParams(); 返回 angular.io 的 URLSearchParams object

import 'rxjs/add/operator/map' 它应该可以工作。

import { URLSearchParams } from '@angular/http';

关于angular - 类型 'URLSearchParams' 不可分配给类型 'URLSearchParams',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43256265/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com