我在将HTTP注入(inject)Angular 2应用程序时遇到问题。几天前,它工作正常,但现在出现错误:
有main.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
import { LoginModule } from "./login/login.module";
import { Http } from "@angular/http";
platformBrowserDynamic().bootstrapModule(LoginModule);
登录模块
@NgModule({
imports: [BrowserModule, FormsModule], // external modules for all components
declarations: [LoginComponent], // component which belong to this module
bootstrap: [LoginComponent] // component on load
})
export class LoginModule {
}
最后是LoginModule中的LoginComponent
import { Component } from '@angular/core';
import { AccountService } from "../data/account.service";
import { Http, Response, RequestOptions, Headers } from '@angular/http';
import { LocalStorage } from '../storage/storage';
@Component({
selector: 'tp-login',
templateUrl: `app/login/login.html`,
styleUrls: ['app/login/login.css'],
providers: [AccountService, LocalStorage]
})
LoginComponent中有一个关于没有HttpProvider的异常。
Somone知道如何解决该问题吗?
最佳答案
可以将所有模块依赖项封装到@ngModule
属性内的主类模块中
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';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
RouterModule.forRoot(appRoutes)
],
providers: [],
bootstrap: [AppComponent]
})
因此,您可以确定何时将您的模块作为子依赖项包含在内-所有依赖关系都将在此之前得到解决。