Phi*_*lon 27 angular2-services angular
我创建了AngularJS 2服务,并在2个不同的组件中使用它:App-Component&Sub-Component.每个输出属性'log'(一个字符串)我的服务.
StateService类:
@Injectable ()
class StateService {
public log : string;
static count : number = 0;
constructor () {
this.log = '';
StateService.count++;
this.writeToLog ('CREATED '+StateService.count+' at ' + new Date().toString());
}
public writeToLog (text : string) : void {
this.log += text + '\n';
}
}
Run Code Online (Sandbox Code Playgroud)
零件 :
@Component ({
selector : 'Sub-Component',
template : `<hr>
This is the Sub-Component !
<BR>
StateService Log :
<pre>{{ _stateService.log }}</pre>
<button (click)="WriteToLog ()">Write to log</button>
`,
providers : [StateService]
})
export class SubComponent {
constructor (private _stateService : StateService) {
}
public WriteToLog () : void {
this._stateService.writeToLog ('From Sub-Component - This is '+new Date().toString());
}
}
Run Code Online (Sandbox Code Playgroud)
这里是代码的实例
我除了创建一次服务之外,当每个组件调用WriteToLog方法时,每个组件的输出都相同,但事实并非如此.
输出示例:
App-Component可以输出:
实例1 - 创建于2016年1月21日星期四11:43:51
来自App-Component - 这是2016年1月21日星期四11:43:54
来自App-Component - 这是2016年1月21日星期四11:43:55
并且子组件可以输出:
实例2 - 创建于2016年1月21日星期四11:43:51
来自子组件 - 这是2016年1月21日星期四11:43:57
从子组件 - 这是2016年1月21日星期四11:43:58
所以似乎创建了2个服务实例(实例1 +实例2)
我只想要一个实例;)当我在日志中追加字符串时,它必须出现在两个组件中.
谢谢您的帮助
Gün*_*uer 32
更新角度> = 2.0.0-RC.6
不要将服务添加到组件的提供者.而是将其添加到
@NgModule({ providers: [...], ...
Run Code Online (Sandbox Code Playgroud)
(由于延迟加载的模块引入了自己的范围,因此未加载延迟的模块)
@Component ({
selector : 'Sub-Component',
template : `<hr>
This is the Sub-Component !
<BR>
StateService Log :
<pre>{{ _stateService.log }}</pre>
<button (click)="WriteToLog ()">Write to log</button>
`,
// providers : [StateService] <== remove
})
Run Code Online (Sandbox Code Playgroud)
角度<= 2.0.0-RC.5
如果将其添加到组件上,则会为每个组件实例获取新的服务实例.而是将其添加到
bootstrap(AppComponent, [StateService]);
Run Code Online (Sandbox Code Playgroud)
您可以通过将其添加到单个组件来进行更细粒度的控制,然后此组件和所有子项都会注入相同的实例,否则应用程序将使用由其创建的实例bootstrap().这是Angulars DI中的"等级".
另见
- http://blog.thoughtram.io/angular/2015/05/18/dependency-injection-in-angular-2.html
- http://blog.thoughtram.io/angular/2015/09/17 /resolve-service-dependencies-in-angular-2.html
| 归档时间: |
|
| 查看次数: |
15370 次 |
| 最近记录: |