【问题标题】:Angular2 rc5: No provider for HttpAngular2 rc5:没有 Http 提供者
【发布时间】:2016-12-18 09:42:18
【问题描述】:

我正在尝试将 Angular 2 应用程序移植到 RC5,但我遇到了一个问题:我无法通过模块正确设置应用程序。

有一个名为 CustomersService 的服务通过 Http 检索客户数据。我创建了一个导入 HttpModule 并提供客户服务的应用程序模块。在另一个名为 HomeModule 的模块中,有一个使用该服务的组件。当应用程序启动时,我在控制台上看到以下错误:

EXCEPTION: Error in ./HomeComponent class HomeComponent_Host - inline template:0:0
ORIGINAL EXCEPTION: No provider for Http!

我不知道为什么会这样……

任何建议将不胜感激。

谢谢。


这里是文件:

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';

import { MdSidenavModule } from '@angular2-material/sidenav';
import { MdListModule } from '@angular2-material/list';
import { MdIconModule } from '@angular2-material/icon';
import { MdToolbarModule } from '@angular2-material/toolbar';
import { MdButtonModule } from '@angular2-material/button';

import { AppComponent } from './app.component';
import { routing } from './app.routing';
import { CustomersService } from './shared/rest_services/customers.service';

import { HomeModule } from './home/home.module';

@NgModule({
    declarations: [AppComponent],
    imports: [
        BrowserModule,
        HttpModule,

        // material design
        MdSidenavModule,
        MdListModule,
        MdIconModule,
        MdToolbarModule,
        MdButtonModule,

        routing,
        // SharedModule.foorRoot(),
        HomeModule
    ],
    providers: [
        CustomersService, Http
    ],
    bootstrap: [AppComponent]
})
export class AppModule {

}

app.component.ts

import { Component } from '@angular/core';
import { CustomersService } from './shared/rest_services/customers.service';

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss']
})
export class AppComponent {
  views = [ {title: 'Home', icon: 'home', path: 'home' } ];

  constructor() {

  }
}

customers.service.ts

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

@Injectable()
export class CustomersService {

  private static _base = 'http://...';

  constructor(private _http: Http) {

  }

  getAll() {
    return this._http.get(CustomersService._base + "/customers.json");
  }
}

home.module.ts

import { NgModule } from '@angular/core';

import { routing }       from './home.routing';
import { HomeComponent } from './home.component';

@NgModule({
    imports: [ routing ],
    declarations: [
        HomeComponent
    ],
    exports: [
        HomeComponent
    ]
})
export class HomeModule {

}

home.component.ts

import { Component, OnInit } from '@angular/core';
import { CustomersService } from '../shared/rest_services/customers.service';

@Component({
  selector: 'app-home',
  templateUrl: 'home.component.html',
  styleUrls: ['home.component.scss']
})
export class HomeComponent implements OnInit {

  constructor(private _customers: CustomersService) {
    this._customers.getAll().subscribe((data) => {
      console.log(data);
    });
  }

  ngOnInit() {
  }

}

【问题讨论】:

标签: angular angular2-http


【解决方案1】:

我认为您不需要在模块的 providers 属性中指定有关 Http 的内容。这是在 imports 属性中包含 HttpModule 时完成的。

@NgModule({
    declarations: [AppComponent],
    imports: [
        BrowserModule,
        HttpModule,

        // material design
        MdSidenavModule,
        MdListModule,
        MdIconModule,
        MdToolbarModule,
        MdButtonModule,

        routing,
        // SharedModule.foorRoot(),
        HomeModule
    ],
    providers: [
        CustomersService // <-----
    ],
    bootstrap: [AppComponent]
})
export class AppModule {

}

HttpModule 为 HTTP 配置相关提供程序。见https://github.com/angular/angular/blob/master/modules/%40angular/http/http.ts#L354

否则,可能是笔误:

import { Http } from '@angular/Http';

而不是

import { Http } from '@angular/http';

【讨论】:

  • 比你!你的救星:)
【解决方案2】:

导入http,然后声明具有Http类型参数的构造函数, 如下所示

import {Http} from "@angular/http";

@Component({......})
export class CompName
{
   ......
constructor(private http: Http)
{
   ....
}

   .....
}

【讨论】:

    猜你喜欢
    • 2016-12-20
    • 2016-12-20
    • 2016-06-13
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 2018-02-22
    • 2016-04-06
    • 2017-01-31
    相关资源
    最近更新 更多