码迷,mamicode.com
首页 > 其他好文 > 详细

Angular2中的Service并不是单例模式

时间:2017-01-14 16:55:41      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:print   lan   component   ted   单例模式   angular   angular2   不同   module   

2015年做了一个使用angularjs 1框架的项目,2016年伊始公司的项目转为使用Angular2框架。

在开发过程中发现了一个坑,这个坑就是在Angular JS 1.x中的Service是单例模式,只要创建了一个Service,那么程序都在使用这唯一的Service。

但是在Angular 2中,Service并不是单例模式,Compnent会从自己向母页寻找Service,直到Module。

如果不同的Component里面都在providers添加了Service,那么这些Component都只会访问自己的Service。

如果想在不同的Component中使用同一个Service,那就找到这两个Component的母页,在母页的providers中加入这个Service。


 

Angular2中的Service并不是单例模式

标签:print   lan   component   ted   单例模式   angular   angular2   不同   module   

原文地址:http://www.cnblogs.com/limark/p/6285502.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!