我使用提供程序在bootstrap中注入服务,如下所示:
@Component({
moduleId: module.id,
selector: 'sd-app',
providers: [TokenService, Principal, HttpClient],
templateUrl: 'app.component.html',
directives: [ROUTER_DIRECTIVES, NavbarComponent]
})
仍然我必须在所有子组件上使用HttpClient提供,我的理解是,在引导期间注入时,将创建服务单例实例,并且在子组件的提供者中为每个子组件提供唯一的服务实例组件将被创建。
我收到错误没有HttpClient的提供程序!如果我不在子组件中使用HttpClient的提供程序 它特定于HttpClient
HttpClient代码是:
@Injectable()
export class HttpClient {
constructor(private _tokenService: TokenService, private router: Router, private principal: Principal) { }
get(url: any, headers: any) {
//body using router, pricipal and _tokenService
}
}
任何帮助!
编辑添加了app.component.ts
import { Component, ViewContainerRef } from '@angular/core';
import { ROUTER_DIRECTIVES, Routes, Router } from '@angular/router';
import { HTTP_PROVIDERS} from '@angular/http';
import { Principal } from './+login/principal';
import { HttpClient } from './shared/http/httpclient';
@Component({
moduleId: module.id,
selector: 'sd-app',
providers: [HTTP_PROVIDERS, Principal, HttpClient],
templateUrl: 'app.component.html',
directives: [ROUTER_DIRECTIVES]
})
@Routes([
{
path: '/',
component: SomeComponent
}
])
export class AppComponent {
private viewContainerRef: ViewContainerRef;
constructor(private _principal: Principal, private _router: Router, viewContainerRef: ViewContainerRef) {
this.viewContainerRef = viewContainerRef;
}
}
** bootstrap **
bootstrap(AppComponent, [
ROUTER_PROVIDERS,
provide(APP_BASE_HREF, { useValue: '<%= APP_BASE %>' })
]);