首页 > 代码库 > [Angular 2] 7. Factory Provider with dependencies
[Angular 2] 7. Factory Provider with dependencies
This lesson discusses when and how to add dependencies, resolved by Angular’s DI, to factory providers. The example used in this lesson builts upon the previous lesson on understanding factory providers.
For example, the LoggerProvider need to use ConsoleService, so when you put LoggerProvider into ‘providers‘ array, we also need to provide dependence which refer to ConsoleService, the way to do this:
providers: [ TodoService, ConsoleService, ,{ provide: LoggerProvider, useFactory: (consoleService) => { return new LoggerProvider(consoleService, true) }, deps: [ConsoleService] } ],
We add ‘deps‘ prop. The order you write into ‘deps‘ also affect the one you inject to useFactory() function:
providers: [ TodoService, ConsoleService, TranslateService, ,{ provide: LoggerProvider, useFactory: (cs, ts) => { return new LoggerProvider(cs, ts, true) }, deps: [ConsoleService, TranslateService] } ],
‘cs‘ is the instance of ‘ConsoleService‘; and ‘ts‘ is the instance of ‘TranslateService‘.
[Angular 2] 7. Factory Provider with dependencies
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。