Inject service inside a service inside a service in my Angular 2 application

半城伤御伤魂 提交于 2020-01-04 07:10:11

问题


Update: I overlooked a service, the problem actually is about a service that injects a service that injects a service. I updated the post.

I am having a hard time injecting a service inside a service in my Angular 2 application, I am not sure if I am interpreting some concepts of dependency injection wrong.

My situation is as follows: I have a parentcomponent called app.component that uses a loginservice to authenticate users, this service is bootstraped in the app bootstrapping process.

I also have a artist component that uses a sharedservice, this service is provided inside my parent component. I want the loginService also injected inside my sharedService, so I can logout a user is a certain action is triggered inside my sharedService.

Somehow, this always returns an error from my sharedService that my loginService is undefined. I thought that the following line should work to inject my loginService inside my sharedService:

constructor(@Inject(LoginService) private _loginService: LoginService,
    private _router: Router, private _http: Http) { }

_loginService still is undefined... Does anybody know what I am doing wrong?

As requested the relevant code snippits:

bootstrap.ts

import { APP_BASE_HREF } from '@angular/common';
import { bootstrap } from '@angular/platform-browser-dynamic';
import { ROUTER_PROVIDERS } from '@angular/router';
import { HTTP_PROVIDERS } from '@angular/http';

import { LoginService } from './+login/login.service';
import { AppComponent } from './app.component';

bootstrap(AppComponent, [
  ROUTER_PROVIDERS,
  HTTP_PROVIDERS,
  LoginService
]);

app.component

import { Component, OnInit } from '@angular/core';
....

@Component({
    moduleId: module.id,
    selector: 'sd-app',
    templateUrl: 'app.component.html',
    directives: [ROUTER_DIRECTIVES, SidebarMenuComponent, PAGINATION_DIRECTIVES, Growl],
    providers: [ArtistService,
        SharedService]
})

artist.component

import { Component, provide, OnInit } from '@angular/core';
....


@Component({
    moduleId: module.id,
    directives: [ FORM_DIRECTIVES, Confirm, RequestListComponent,
    QuotationListComponent, TOOLTIP_DIRECTIVES, SELECT_DIRECTIVES ],
    pipes: [ MapToIterablePipe, DayMonthYearTimePipe ],
    templateUrl: 'artist-detail.html'
})

export class ArtistComponent implements OnInit {

    constructor(
        private _artistService: ArtistService,
        public sharedService: SharedService
    ) { }

artist.service

import { Injectable } from '@angular/core';
import { SharedService } from '../shared/shared.service';

@Injectable()
export class ArtistService {

    constructor(private _sharedService: SharedService) {
    }

shared.service.ts

import { Injectable, Inject } from '@angular/core';
import { Router } from '@angular/router';
import { Headers, Http, RequestOptions } from '@angular/http';

import { LoginService } from '../+login/login.service';

@Injectable()
export class SharedService {

    constructor(@Inject(LoginService) private _loginService: LoginService,
        private _router: Router, private _http: Http) { }

    logoutUser(objectType: string, objectID: string) {
        this._sharedService.logout();
    }

来源:https://stackoverflow.com/questions/37836376/inject-service-inside-a-service-inside-a-service-in-my-angular-2-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!