没有Http的提供者

mie*_*ooy 8 typescript angular

我有将问题注入Angular 2应用程序的问题.几天前它工作正常,但现在我有错误:

原始例外:没有Http的提供商!

有主要的

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);
Run Code Online (Sandbox Code Playgroud)

登录module.ts

@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 {
}
Run Code Online (Sandbox Code Playgroud)

最后在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]

})
Run Code Online (Sandbox Code Playgroud)

LoginComponent中没有关于没有HttpProvider的异常.Somone知道如何解决这个问题?

Vad*_*imB 11

很好地将所有模块依赖项封装到@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]
})
Run Code Online (Sandbox Code Playgroud)

因此,您可以确定当您的模块将作为子依赖项包含时 - 所有依赖项将在之前解决.


Fal*_*aly 3

首先,从“./login/login.module”中删除 import { LoginModule } ;在你的 main.ts 中,它不能解决你的问题。

尝试将HttpModule导入到您的登录模块文件或根模块中:

import { HttpModule } from '@angular/http';
@NgModule({
    imports: [HttpModule, BrowserModule, FormsModule], 
    declarations: [LoginComponent], // component which belong to this module
    bootstrap: [LoginComponent] // component on load
})
export class LoginModule {
}
Run Code Online (Sandbox Code Playgroud)