Browse Source

@Self裝飾器

指定使用當下注入器,若當前未設定,則會拋出例外
master
HarveyChou 2 years ago
parent
commit
8fd056e445
  1. 9
      src/app/child/child.component.ts

9
src/app/child/child.component.ts

@ -1,12 +1,13 @@
import { Component, Inject, SkipSelf } from '@angular/core';
import { Component, Inject, Self, SkipSelf } from '@angular/core';
@Component({ @Component({
selector: 'app-child', selector: 'app-child',
templateUrl: './child.component.html', templateUrl: './child.component.html',
styleUrls: ['./child.component.css'], styleUrls: ['./child.component.css'],
providers: [{ provide: 'Message', useValue: '定義在子元件的訊息' }],
// 未設定時會拋出錯誤
//providers: [{ provide: 'Message', useValue: '定義在子元件的訊息' }],
}) })
export class ChildComponent { export class ChildComponent {
//如果拿走@SkipSelf(),則會顯示子元件的訊息
constructor(@SkipSelf() @Inject('Message') public message: string) {}
// 指定 @Self 只會找尋自己的提供者實體
constructor(@Self() @Inject('Message') public message: string) {}
} }
Loading…
Cancel
Save