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