Angular2"服务"如何将一个服务注入另一个服务(单身人士)?

N.S*_*per 27 angular

我如何将一项服务注入另一项服务?例如,假设我有一个需要另一个Collection(TeamCollection => PlayerCollection)的Collection.目前我只创建两个单独的集合,并使用类似的东西:

import {PlayerCollection} from "<<folder>>/player";
Run Code Online (Sandbox Code Playgroud)

但是这需要我在Typescript中为我想要成为单例实例的每个服务编写自己的单例getInstance代码.

这样做的正确方法是什么?我希望在我的组件中拥有两个单例,并且能够使用构造函数语法将一个服务注入另一个服务,而无需创建单例的新实例.

class TeamCollection {    
    constructor(@Inject(PlayerCollection): PlayerCollection) {}
}
Run Code Online (Sandbox Code Playgroud)

N.S*_*per 16

所以重新阅读Pascal Precht的这篇优秀文章后:http://blog.thoughtram.io/angular/2015/05/18/dependency-injection-in-angular-2.html

并看到他评论:http://twofuckingdevelopers.com/2015/04/angular-2-singleton-service/

"使用Angular 2的DI注入的所有内容都已经是Singleton.不需要这样的服务"

我去测试了,我现在发现的都回答了我的问题,让我对angular2中的DI话题更加困惑.

请参阅以下代码:

team.ts

import {BaseCollection, BaseModel} from "./base";
import {PlayerCollection} from './player';
import {Injectable, Inject} from "angular2/angular2";

@Injectable()
export class TeamCollection extends BaseCollection {
    playerCollection: PlayerCollection;
    constructor(@Inject(PlayerCollection) playerCollection: PlayerCollection) {
        super();
        this.playerCollection = playerCollection;
    }

    create(data: Object): TeamModel {
        return new TeamModel(data);
    }
}
Run Code Online (Sandbox Code Playgroud)

player.ts

import {BaseCollection, BaseModel} from "./base";
import {Injectable} from "angular2/angular2";

@Injectable()
export class PlayerCollection extends BaseCollection {
    create(data: Object): PlayerModel {
        return new PlayerModel(data);
    }
}
Run Code Online (Sandbox Code Playgroud)

team.spec.ts

/// <reference path="../../typings.d.ts" />

//VERY IMPORTANT TO ALWAYS LOAD THESE
import 'zone.js';
import 'reflect-metadata';
import 'es6-shim';

import {TeamModel, TeamCollection} from "../../app/model/team";
import {PlayerCollection} from "../../app/model/player";
import {Inject, Injector} from "angular2/angular2";

describe('TeamCollection', () => {
  var teamCollection: TeamCollection;
  var playerCollection: PlayerCollection; 
  beforeEach(() => {
      var injector = Injector.resolveAndCreate([
        TeamCollection,
        PlayerCollection
      ]);
      teamCollection = injector.get(TeamCollection);  

      var injectorT = Injector.resolveAndCreate([
        PlayerCollection
      ]);
      playerCollection = injector.get(PlayerCollection);
  });

  it('should have a singleton PlayerCollection shared between all classes within the application', () => {
    console.log(teamCollection.playerCollection.uuId);
    console.log(playerCollection.uuId);
  });  
});
Run Code Online (Sandbox Code Playgroud)

只要var injector创建它们的Injector()相同,它们共享相同的uuID虽然当我使用第二个注入器(var injectorT)时UUID是不同的意味着创建了playerCollection的新实例.

现在我的问题是.如果我使用组件提供程序语法:

@Component({
  selector: 'app',
  providers: [TeamCollection]
}) 

@Component({
  selector: 'player-list',
  providers: [PlayerCollection]
})
Run Code Online (Sandbox Code Playgroud)

两者会共享同一个玩家集合还是会创建一个新实例?

编辑: 只要通过该bootstrap(.., [ServiceA,ServiceB])方法创建它们就会执行.

感谢pascal precht http://blog.thoughtram.io/angular/2015/09/17/resolve-service-dependencies-in-angular-2.html