• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

151
Views
El tipo 'URLSearchParams' no se puede asignar al tipo 'URLSearchParams'

Quiero enviar una solicitud http.get con algunos parámetros de búsqueda a mi webapi para obtener una lista de estudiantes. Encontré algunos ejemplos sobre cómo hacer esto, pero después de hacer exactamente lo mismo que en los ejemplos, aparece este extraño error:

 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'.

Aquí está mi componente:

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

¿Qué podría estar causando este error?

almost 3 years ago · Santiago Trujillo
2 answers
Answer question

0

Parece que el URLSearchParams nativo se declara en su código actual, mientras que el new URLSearchParams(); devuelve el objeto URLSearchParams de angular.io

import 'rxjs/add/operator/map' y debería funcionar.

 import { URLSearchParams } from '@angular/http';
almost 3 years ago · Santiago Trujillo Report

0

Intenta usar el método 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- }
almost 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error