2017-04-06 5 views
19

मैं छात्रों की सूची प्राप्त करने के लिए अपने वेबपैई में कुछ खोज पैरामीटर के साथ http.get अनुरोध भेजना चाहता हूं। मैं ऐसा करने के तरीके पर कुछ उदाहरण मिल गया, लेकिन उदाहरण के रूप में वास्तव में करने के बाद, मैं इस अजीब त्रुटि मिलती है:'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- 
} 
} 

क्या इस त्रुटि का कारण हो सकता?

+2

लगता है कि आपने [देशी URLSearchParams] उपयोग करने के लिए कोशिश कर रहे हैं (https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)। आपको [कोणीय एक] (https://angular.io/docs/ts/latest/api/http/index/URLSearchParams-class.html) का उपयोग करके '@ कोणीय/http'' –

उत्तर

26

का उपयोग कर ऐसा लगता है देशी URLSearchParams अपने वर्तमान कोड के लिए घोषित किया जाता है की कोशिश करो, जबकि new URLSearchParams(); रिटर्न angular.io के URLSearchParams object

आयात 'rxjs/add/operator/map' और यह काम करना चाहिए।

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

से आयात करके इसका उपयोग करना चाहिए आदमी। बस एक साधारण आयात ... –

1

सेट विधि

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- 
} 
संबंधित मुद्दे