No provider for Response in angular 2 even thought i have added providers in component?

前端 未结 2 344
暗喜
暗喜 2021-01-28 02:51

Error: This is the error I am getting even thought I have added provider to mine component.I am unable to get what\'s the mine error for. here are mine all file app.component.t

相关标签:
2条回答
  • 2021-01-28 02:59

    You don't need to provide the Response, it's a reference generated by the Http itself. In the early days of alpha version I faced the same error and solved it using this line: constructor(@Inject(Http) http : Http). In the constructor of your CarService just add @Inject and tell us if it solves your problem.

    carservice.ts becomes:

    import { Inject, Injectable } from '@angular/core';
    import { Http, Response } from '@angular/http';
    
    
    @Injectable()
    export class CarService {
    
        constructor(@Inject(Http) http : Http) {}
    
        getCarsMedium() {
            return this.http.get('app/resources/data/cars-medium.json')
                        .toPromise()
                        .then(res => <Car[]> res.json().data)
                        .then(data => { return data; });
        }
    }
    export interface Car {
       label;
       value;
    }
    
    0 讨论(0)
  • 2021-01-28 03:09

    Response depends on ResponseOptions and ResponseOptions is not designed in a way it can be injected easily. See https://github.com/angular/angular/blob/bb8976608db93b9ff90a71187608a4390cbd7a07/modules/%40angular/http/src/base_response_options.ts#L57

    If you are injecting Response somewhere then you're probably doing something that is not supposed to work this way.

    0 讨论(0)
提交回复
热议问题