我正在将我的应用程序迁移到最新的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 =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未处理的Promise拒绝:没有HTTPService的提供者!; 区域:角; 任务:Promise.then; 值:NoProviderError {__zone_symbol__error:错误:NoProviderError.ZoneAwareError上的DI错误(http:// localhost:4200/vendor.bundle.js:87143:...,_ nativeError:ZoneAwareError,keys:Array [1],injectors:Array [1] ,__ zone_symbol__message:"没有HTTPService的提供者!"...} __ zone_symbol__error:错误:NoProviderError.ZoneAwareError上的DI错误
可能对你来说很简单,但从这里我看不出问题.谢谢.
您需要将您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 { }