例子:
import { Injectable } from '@angular/core';
let count = 0;
@Injectable()
export class MyService {
seed: number;
_count: number;
constructor(){
this.seed = Number((Math.random() * 100).toFixed(0));
this._count = count++;
console.log('diablo constructor called: ' + this.seed);
}
}
这个单例行为在Angular DI帮助文档里有提到:
https://angular.io/guide/glossary#injector