博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Angular] Control the dependency lookup with @Host, @SkipSelf and @Optional
阅读量:6904 次
发布时间:2019-06-27

本文共 2732 字,大约阅读时间需要 9 分钟。

Very differently to AngularJS (v1.x), Angular now has a hierarchical dependency injector. That allows to specify service definitions as well as the  at various levels in our application. Whenever a service is requested, Angular will walk up the component tree and search for a matching definition. While in most cases that's perfectly fine, often you may want to take control over the dependency lookup. In this lesson we will learn how, by applying @Host()@SkipSelf() and @Optional().

 

@Optional:

When using @Optional, it set logger to null if the LoggerService is not provided instead of error out.

export class PersonComponent implements OnInit {  constructor(    @Optional()    public logger: LoggerService  ) {}  ngOnInit() {}  doLog() {    if (this.logger) {      this.logger.log('Message from PersonComponent');    } else {      console.log('sorry, no logger available');    }  }}

 

@SkipSelf:

If child component and parent component both using the same provider and we want to skip using child component's provider instead using parent's provider.

// Child@Component({  selector: 'app-person',  template: `    

I don't have a logger

` providers: [ { provide: LoggerService, useFactory: loggerFactory('PersonComponent') } ]})export class PersonComponent implements OnInit { constructor( @SkipSelf() @Optional() public logger: LoggerService ) {} ngOnInit() {} doLog() { if (this.logger) { this.logger.log('Message from PersonComponent'); } else { console.log('sorry, no logger available'); } }}
// parent@Component({  selector: 'app-root',  template: `    

Angular Services

`, providers: [ { provide: LoggerService, useFactory: loggerFactory('AppComponent') } ]})export class AppComponent {}

SO in the end 'AppComponent ...' log message will appear in the console.

 

@Host():

Only using the provider for its own component.

@Component({  selector: 'app-person',  template: `    

I don't have a logger

` // providers: [ // { // provide: LoggerService, // useFactory: loggerFactory('PersonComponent') // } // ]})export class PersonComponent implements OnInit { constructor( @Host() @Optional() public logger: LoggerService ) {} ngOnInit() {} doLog() { if (this.logger) { this.logger.log('Message from PersonComponent'); } else { console.log('sorry, no logger available'); } }}

So if PersonComponent has provider defined, it will use its own provider and will not continue searching parent component. 

Often @Host can use togerther with @Optional, so if the provider is not defined, then set it to null.

 

转载地址:http://tsldl.baihongyu.com/

你可能感兴趣的文章
vpc的使用方法
查看>>
GitExtensions GitCredentialWinStore syntax error near unexpected token `('
查看>>
Java获取EXE文件图标的方法
查看>>
“驱动程序在 \Device\Harddisk0\D 上检测到控制器错误”的根本解决办法!
查看>>
ubuntu 之修改权限的问题
查看>>
php 框架ci去index.php的方法
查看>>
Hyper-v学习(四),SMB虚拟机实时迁移
查看>>
基于spring3注解的google分页
查看>>
实用命令行工具详解—crontab
查看>>
redis+ssh-keygen免认证登录案例
查看>>
linux svn命令
查看>>
Android中获取CPU负载和进程cpu时间
查看>>
docker容器启动后添加端口映射
查看>>
Xtrabackup系列之:二进制安装
查看>>
python变量的定义
查看>>
Python面向对象
查看>>
CentOS5 安装vsFtpd软件及配置
查看>>
设计师应该关注的科技发展方向(二)
查看>>
redis python监控
查看>>
PowerDesigner连接MySQL数据库
查看>>