未处理的承诺拒绝:没有HTTPService的提供者

时间:2017-01-26 13:09:19

标签: angular typescript angular2-routing angular2-services

我正在将我的应用程序迁移到最新的RC并且遇到一些我无法修复的错误。在决定问这里之前,我做了一个彻底的搜索,但没有任何运气。

因此,按下按钮时,应加载以下内容:

以下是组件

import { Component,OnInit } from '@angular/core';

import { HTTPService } from '../shared/api.service';
import { Server } from './server.model';
import { SharedService } from '../shared/moveData.service';


@Component({
    templateUrl: './mainserverpage.template.html',
})
export class MainServerPage implements OnInit {

    constructor(private _httpService: HTTPService, 
                private _moveData: SharedService) { }

    errorMessage: string;
    public servers: Server[];
    isLoading = true;
    tillLoading;
    selectedServer: Server;
    currentServer;
    isServerOnline=false;

    ngOnInit() {
          this.getServers('qa');
    }

    reloadServers(env) {
        this.servers = null;

        this.getServers(env);
    }

    getServers(env?) {
        this._httpService.getServers(env)
            .subscribe(
            value => {
                this.servers = value;
                this.isLoading = false;
            },
            error => this.errorMessage = <any>error);
    }
}

模块非常基本:

import { NgModule }            from '@angular/core';
import { CommonModule }        from '@angular/common';
import { HttpModule }          from '@angular/http';

import { SharedModule }        from '../shared/shared.module';

import { MainServerPage }      from './mainserverpage.component';
import { Server }         from './server.model';
import {HTTPService} from '../shared/api.service';

@NgModule({
    imports: [
        CommonModule,
        SharedModule,
        HttpModule,
    ],
    declarations: [
        MainServerPage
    ],
    exports: [
        MainServerPage
    ],
    providers: [
        HTTPService
    ]
})
export class MainserverpageModule {
}

路由

import { RouterModule  }     from '@angular/router';

import { MainServerPage }    from './mainserverpage.component';


export const mainRouting = RouterModule.forChild([
    { path: 'mainserverpage', component: MainServerPage }
]);

最后我正在使用 app.module 。我认为添加模板毫无意义。

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

import { AppComponent } from './app.component';
import { SharedModule } from '../app/shared/shared.module';
import { MainserverpageModule } from '../app/servers/mainserverpage.module';

import { HomeComponent }     from './home.component';
import { NavBarComponent }   from './navbar.component';
import { NotFoundComponent } from '../app/shared/notfound.component';
import { MainServerPage } from '../app/servers/mainserverpage.component';

import { routing }           from './app.routing';
import { mainRouting } from '../app/servers/mainserverpage.routing';

@NgModule({
  declarations: [
    AppComponent,
    NavBarComponent,
    HomeComponent,
    NotFoundComponent,
    MainServerPage
  ],
  imports: [
    BrowserModule,
    FormsModule,
      HttpModule,
    SharedModule,
    routing,
    mainRouting
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

以下是我尝试加载时收到的错误:

  

zone.js:420未处理的承诺拒绝:没有HTTPService的提供者!   ;区域:角;任务:Promise.then;值:NoProviderError   {__zone_symbol__error:错误:DI错误       在NoProviderError.ZoneAwareError(http://localhost:4200/vendor.bundle.js:87143:...,_ nativeError:   ZoneAwareError,keys:Array [1],injectors:Array [1],   __zone_symbol__message:“没有HTTPService的提供者!”...} __ zone_symbol__error:错误:DI错误       在NoProviderError.ZoneAwareError

对你来说可能很简单但是从这里我看不到问题。 谢谢。

2 个答案:

答案 0 :(得分:9)

您需要将MainserverpageModule添加到app.module个导入中。目前,您只包含MainServerPage组件,而不是您的服务。

@NgModule({
  declarations: [
    AppComponent,
    NavBarComponent,
    HomeComponent,
    NotFoundComponent
    //Remove the component
  ],
  imports: [
    BrowserModule,
    MainserverpageModule <-- Add the module
    FormsModule,
    HttpModule,
    SharedModule,
    routing,
    mainRouting
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

答案 1 :(得分:3)

您需要使用

告诉您的组件有关提供商的信息
import { SharedService } from '../shared/moveData.service';

@Component({
    templateUrl: './mainserverpage.template.html',
    providers: [SharedService]   <= inject your services here
})
export class MainServerPage implements OnInit {

    constructor(private _httpService: HTTPService, 
                private _moveData: SharedService) { }

}