【问题标题】:No provider for Response in angular 2 even thought i have added providers in component?即使我在组件中添加了提供者,也没有 Angular 2 中的响应提供者?
【发布时间】:2016-06-03 11:23:39
【问题描述】:

错误:这是我什至认为我已将提供程序添加到我的组件的错误。我无法了解我的错误是什么。这是我的所有文件 app.component.ts、app.component.html、carservice.ts。 我无法解决。

 EXCEPTION: Error in :0:0
    ORIGINAL EXCEPTION: No provider for Response!
    ORIGINAL STACKTRACE:
    error: DI Exception
    platform-browser.umd.js:962 Error: DI Exception
        at NoProviderError.BaseException [as constructor] (core.umd.js:3776)
        at NoProviderError.AbstractProviderError [as constructor] (core.umd.js:4307)
        at new NoProviderError (core.umd.js:4342)
        at ReflectiveInjector_._throwOrNull (core.umd.js:5794)
        at ReflectiveInjector_._getByKeyDefault (core.umd.js:5822)
        at ReflectiveInjector_._getByKey (core.umd.js:5785)
        at ReflectiveInjector_.get (core.umd.js:5594)
        at DebugAppView._View_IntegratedWorkshop_Host0.createInternal (IntegratedWorkshop_Host.template.js:29)
        at DebugAppView.AppView.create (core.umd.js:9862)
        at DebugAppView.create (core.umd.js:10054)

app.component.ts

import {Component} from '@angular/core';
import {HTTP_PROVIDERS} from '@angular/http';
import {InputText,DataTable,Button,Dialog,Column,Header,Footer,Dropdown,SelectItem} from 'primeng/primeng';

import {CarService} from './cars/carservice';

@Component({
    templateUrl: 'app/app.component.html',
    selector: 'my-app',
    directives: [InputText,DataTable,Button,Dialog,Column,Header,Footer,Dropdown],
    providers: [HTTP_PROVIDERS,CarService]
})
export class AppComponent{
  selectedCity: string;
  cars: SelectItem[];

    constructor(private carService: CarService) { 
         this.cars = [];
         this.cars.push({label: "label", value: "value"});
    }

    ngOnInit() {
        this.carService.getCarsMedium().then(cars => this.cars = cars);
    }
}

app.component.html:

<div >

   <p-dropdown [options]="cars" [(ngModel)]="selectedCity"></p-dropdown>
   <p>Selected City: {{selectedCity||'none'}}</p>

</div>

汽车服务.ts:

import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';


@Injectable()
export class CarService {

    constructor(private 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;
}

main.ts

import {bootstrap} from '@angular/platform-browser-dynamic';
import {AppComponent} from './app.component';
import 'rxjs/Rx';

bootstrap(AppComponent);

汽车-medium.json

{
    "data":[
       {"label":"Mercedez", "value":"Mercedez"},
       {"label":"BMW", "value":"BMW"},
       {"label":"Lam", "value":"Lam"},
       {"label":"Ista", "value":"Ista"},
       {"label":"Ferrari", "value":"Ferrari"}
    ]
}

【问题讨论】:

  • 你在哪里使用响应?
  • 形成json对象数据。
  • 我在你的代码中看不到
  • 对不起,我忘了添加现在你可以看到了。
  • 尝试改变 import {HTTP_PROVIDERS} from '@angular/http';到 main.ts 并设置 bootstrap(AppComponent, [HTTP_PROVIDERS])

标签: typescript angular


【解决方案1】:

Response 依赖于ResponseOptions,而ResponseOptions 的设计方式并非易于注入。见https://github.com/angular/angular/blob/bb8976608db93b9ff90a71187608a4390cbd7a07/modules/%40angular/http/src/base_response_options.ts#L57

如果您在某处注入 Response,那么您可能正在做一些不应该以这种方式工作的事情。

【讨论】:

  • 这意味着我将我的 .json 文件(即响应)放在哪里不是我项目中定义的正确位置?
  • 您的代码没有显示您在何处或如何使用Response,因此很难判断它是否是正确的地方。我不确定“我将 .json 放在哪里”是什么意思。 json文件与错误信息和Response有什么关系?
  • 也许,我的项目结构是这样的,所以我不能把我自己的 .json 文件放在我的项目中,可能有一些特定的地方。你的想法是什么?
  • 错误消息显示“没有响应提供者”。我不知道您如何将此错误消息与放置 json 文件的位置相关联。您导入Response 为什么不提供有关如何以及在何处使用它的信息?
  • 我使用这个响应来响应我的下拉框,如使用primeng组件所示
【解决方案2】:

您不需要提供响应,它是由 Http 本身生成的引用。在 alpha 版本的早期,我遇到了同样的错误并使用以下行解决了它:constructor(@Inject(Http) http : Http)。 在 CarService 的构造函数中添加 @Inject 并告诉我们它是否解决了您的问题。

carservice.ts 变成

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

【讨论】:

  • 谢谢,但我得到了我刚刚添加的解决方案 var jsonFiles = 'src/app/**/*.json'; gulp.src(jsonFiles).pipe(gulp.dest('build/app')).pipe(gulp.dest(env.deployPath + '/app'));到我的 gulp-typescript.js 并且它有效
  • 您是使用 gulp-typescript 进行开发还是仅用于生产?很奇怪,因为错误本身与项目结构没有太大关系。
猜你喜欢
  • 2018-04-11
  • 1970-01-01
  • 2017-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-20
  • 2015-07-26
  • 2018-01-09
相关资源
最近更新 更多