首页 > 代码库 > [Angular 2] 6. Factory Provider
[Angular 2] 6. Factory Provider
In this lesson, we discuss how and when to use factory providers, to enable dependencies that shouldn’t be available to Angular’s DI.
If you have this service:
export class LoggerProvider { constructor(enabled: boolean){ if(enabled){ console.log("Logger is enabled") } }}
Which requires you provide param in constructor function.
If you just use it like normal provider:
@Component({ selector: ‘todos‘, providers: [TodoService, LoggerProvider], template: `...` })
It will NOT work, because when init the LoggerProvider instance, we need to pass a boolean value as a param.
But you can use factory provider for this:
providers: [ TodoService ,{ provide: LoggerProvider, useFactory: () => { return new LoggerProvider(true) } } ],
[Angular 2] 6. Factory Provider
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。