简体   繁体   English

没有Http的提供者

[英]No provider for Http

I have issue with injecting HTTP into Angular 2 application. 我有将问题注入Angular 2应用程序的问题。 Few days ago it was working fine but now I have error: 几天前它工作正常,但现在我有错误:

ORIGINAL EXCEPTION: No provider for Http! 原始例外:没有Http的提供商!

There is 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);

Login module.ts 登录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 {
}

And finally LoginComponent in 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]

})

There is exception in LoginComponent about no HttpProvider. LoginComponent中没有关于没有HttpProvider的异常。 Somone know how to solve that issue ? Somone知道如何解决这个问题?

Good to incapsulate all your module dependencies into main class module inside @ngModule attribute 很好地将所有模块依赖项封装到@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]
})

So you can be sure that when your module will be included as child dependency - all dependecies will be resolved before. 因此,您可以确定当您的模块将作为子依赖项包含时 - 所有依赖项将在之前解决。

In the App Module. 在应用程序模块中。

Import HttpModule and Http from @angular/http : @angular/http导入HttpModuleHttp

import {HttpModule, RequestOptions, XHRBackend, Http} from "@angular/http";

Add the HttpModule to import property in @NgModule declarations: @NgModule声明中添加HttpModule以导入属性:

imports: [
    HttpModule,
]

Provide Http in Providers property of @NgModule @NgModule Providers属性中提供Http

providers: [
    {provide: Http, useFactory: (backend: XHRBackend, defaultOptions: RequestOptions) => new Http(backend, defaultOptions),rations
        deps: [XHRBackend, RequestOptions]}
],

First, remove the import { LoginModule } from "./login/login.module"; 首先,从“./login/login.module”中删除import {LoginModule}; in your main.ts, it doesn't solve your problem. 在你的main.ts中,它不能解决你的问题。

Try importing HttpModule into your Login module file or in your root module: 尝试将HttpModule导入Login模块文件或根模块:

 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 { } 

Add HttpModule in your component/ service file : component/服务文件中添加HttpModule

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

and then add it HttpModule in app.module.ts : 然后在app.module.ts添加它HttpModule

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

and in ngModules imports also 并在ngModules导入

imports: [
    BrowserModule,
    HttpModule   ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM