甚至以为我在组件中添加了提供程序,甚至没有在角度2中提供Response的提供程序? [英] No provider for Response in angular 2 even thought i have added providers in component?

查看:57
本文介绍了甚至以为我在组件中添加了提供程序,甚至没有在角度2中提供Response的提供程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误:这是我什至以为我已将提供程序添加到我的组件中的错误.我无法获得我的错误是什么.这是我的所有文件 app.component.ts,app.component.html,carservice.ts. 我无法解决.

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.ts, app.component.html, carservice.ts. I am unable to solve.

 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

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:

app.component.html :

<div >

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

</div>

carservice.ts:

carservice.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);

cars-medium.json

cars-medium.json

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

推荐答案

您不需要提供Response,它是Http本身生成的引用.在早期的Alpha版本中,我遇到了相同的错误,并使用以下代码行解决了该问题:constructor(@Inject(Http) http : Http). 在CarService的构造函数中,只需添加@Inject并告诉我们是否可以解决您的问题.

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 成为:

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

这篇关于甚至以为我在组件中添加了提供程序,甚至没有在角度2中提供Response的提供程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆