Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将服务注入到Angular 2应用程序中的服务中_Angular_Angular2 Services - Fatal编程技术网

将服务注入到Angular 2应用程序中的服务中

将服务注入到Angular 2应用程序中的服务中,angular,angular2-services,Angular,Angular2 Services,更新:我忽略了一个服务,问题实际上是关于一个注入服务的服务。我更新了帖子。 在Angular 2应用程序中,我很难在服务中注入服务,我不确定我是否错误地解释了依赖注入的一些概念 我的情况如下: 我有一个名为app.component的parentcomponent,它使用loginservice对用户进行身份验证,该服务在应用程序引导过程中引导 我还有一个使用sharedservice的Artister组件,该服务在我的父组件中提供。我希望登录服务也注入到我的共享服务中,这样当某个操作在我的共享

更新:我忽略了一个服务,问题实际上是关于一个注入服务的服务。我更新了帖子。

在Angular 2应用程序中,我很难在服务中注入服务,我不确定我是否错误地解释了依赖注入的一些概念

我的情况如下: 我有一个名为app.component的parentcomponent,它使用loginservice对用户进行身份验证,该服务在应用程序引导过程中引导

我还有一个使用sharedservice的Artister组件,该服务在我的父组件中提供。我希望登录服务也注入到我的共享服务中,这样当某个操作在我的共享服务中触发时,我就可以注销用户

不知何故,这总是从我的sharedService返回一个错误,即我的loginService未定义。我认为下面这行应该可以将我的loginService注入我的sharedService中:

constructor(@Inject(LoginService) private _loginService: LoginService,
    private _router: Router, private _http: Http) { }
_登录服务仍未定义。。。有人知道我做错了什么吗

根据要求,相关代码包括:

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
]);
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();
    }
应用程序组件

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]
})
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
    ) { }
艺术家组件

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]
})
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
    ) { }
艺术家服务

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

@Injectable()
export class ArtistService {

    constructor(private _sharedService: SharedService) {
    }
shared.service.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
]);
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();
    }

为什么图形显示的服务名称以小写字母开头,而构造函数以大写字母开头。看到你添加的(漂亮的)图形的实际代码会更有帮助。我会在帖子中添加我的代码,我想这会让它更容易理解:)我认为你的理解是正确的,但你的代码中肯定有一些错误。你在哪里提供和注入
共享服务
?我监督了一项服务,我更新了一个帖子(它实际上是一个服务中的一个服务中的一个服务)。我的SharedService在应用程序组件中提供,它被注入我的ArtistService中。