2017-04-06 14 views
19

Chcę wysłać do mojego webapi żądanie http.get z niektórymi parametrami wyszukiwania, aby uzyskać listę studentów. Znalazłem kilka przykładów, jak to zrobić, ale po zrobieniu dokładnie tak, jak w przykładach, otrzymuję ten dziwny błąd:Nie można wpisać "URLSearchParams", aby wpisać "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'. 

Oto mój komponent:

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- 
} 
} 

Co może być przyczyną tego błędu?

+2

Wygląda na to, że próbujesz użyć [rodzimych URLSearchParams] (https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams). Powinieneś używać [Angularowego] (https://angular.io/docs/ts/latest/api/http/index/URLSearchParams-class.html) przez zaimportowanie go z '@ @ kąt/http'' –

Odpowiedz

26

Wydaje rodzimą URLSearchParams jest zadeklarowana do niniejszego kodeksu, natomiast new URLSearchParams(); Zwraca angular.io za URLSearchParams object

importu 'rxjs/add/operator/map' i powinno działać.

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

dzięki człowiek. po prostu prosty import ... –

1

Spróbuj użyć ustawioną metodę

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- 
} 
Powiązane problemy