简体   繁体   English

错误:没有Http的提供程序! (MyService-> Http)

[英]Error: No provider for Http! (MyService -> Http)

I'm using angular 2. 我正在使用角度2。

@Injectable()
export class MyService {
    constructor(private http: Http, @Inject('name') @Optional() public name?: string) {            
}

inside appModule I'm trying to use provider for my service 在appModule内部,我正在尝试使用提供程序进行服务

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



 @NgModule({
  bootstrap: [App],
  declarations: [
    App    
  ],
  imports: [ // import Angular's modules    
    BrowserModule,
    HttpModule
 ],
  providers: [    
    Http, // if I comment this out I'm getting ERROR Error: No provider for 
          // Http! (MyService -> Http)
    MyService, 
    [
      {  provide: 'name', useValue: ''  }      
    ],
....,

Inside AppComponent I'm using Reflective injector 在AppComponent内部,我正在使用反射式注入器

const injector = ReflectiveInjector.resolveAndCreate(
      [MyService,
        {
          provide: 'name', useValue: 'my name'
        }
      ]);

But I'm getting case one: Inside providers if I have following 但是我遇到了第一种情况:如果我有以下内容,请查看内部提供者

providers: [ Http ]

then I'm getting 然后我得到

Unhandled Promise rejection: No provider for ConnectionBackend! 未处理的承诺拒绝:没有ConnectionBackend的提供者! ; ; Zone: ; 区域: Task: Promise.then ; 任务:Promise.then; Value: Error: No provider for ConnectionBackend! 值:错误:没有ConnectionBackend的提供者!

and if left Http out, providers: [ ] I'm getting 如果不使用Http,则providers: [ ]我正在

Error: No provider for Http! 错误:没有Http的提供程序! (MyService -> Http) (MyService-> Http)

On actual service I'm having constructor like 在实际服务中,我有像

@Injectable()
export class MyService {
    constructor(private http: Http, @Inject('name') @Optional() public name?: string) {

    }

Update: inside AppComponent I'm injecting constructor(private parentInjector:Injector){ } 更新:在AppComponent内部,我正在注入构造函数(私有parentInjector:Injector){}

const injector = ReflectiveInjector.resolveAndCreate(
      [MyService,
        {
          provide: 'name', 'abc'
        }
      ], this.parentInjector);

    this.security = injector.get(MyService);

Unhandled Promise rejection: No provider for ConnectionBackend! 未处理的承诺拒绝:没有ConnectionBackend的提供者! ; ; Zone: ; 区域: Task: Promise.then ; 任务:Promise.then; Value: Error: No provider for ConnectionBackend! 值:错误:没有ConnectionBackend的提供者!

将此模块用于Angular的最新版本:

import { HttpClient, HttpHeaders } from '@angular/common/http';

You need to pass the parent injector (from the Angular application), otherwise the created injector will only know the providers you pass explicitely 您需要传递父注入器(来自Angular应用程序),否则创建的注入器将仅明确知道您传递的提供者

constructor(private parentInjector:Injector) {

}
 ...

 const injector = ReflectiveInjector.resolveAndCreate(
  [MyService,
    {
      provide: 'name', useValue: 'my name'
    }
  ], this.parentInjector);

See also https://angular.io/api/core/ReflectiveInjector#resolveAndCreate 另请参见https://angular.io/api/core/ReflectiveInjector#resolveAndCreate

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

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