Angular InjectionToken的一个具体使用例子
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Angular InjectionToken的一个具体使用例子
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                導入InjectionToken:
import {InjectionToken} from '@angular/core';調用從@Angular/core導入的構造器,創建一個新的實例:
export const TOKEN_HOST_CLASS_PROVIDER = new InjectionToken<HostTokenComponentService>('TOKEN_HOST_CLASS_PROVIDER');Injection構造器的實現:
export declare class InjectionToken<T> {protected _desc: string;readonly ?prov: never | undefined;constructor(_desc: string, options?: {providedIn?: Type<any> | 'root' | 'platform' | 'any' | null;factory: () => T;});toString(): string; }Component構造器里的定義:
providers: [HostComponentService,{provide: TOKEN_HOST_CLASS_PROVIDER, useClass: HostTokenComponentService}]在Component 構造函數里進行注入:
export class HostDecoratorComponent {constructor(private hostComponentService: HostComponentService, @Inject(TOKEN_HOST_CLASS_PROVIDER) h){console.log('in HostDecoratorComponent, Host component service got from own Injector: ', hostComponentService, ' HostTokenComponentService: ', h);h.print();}最后運行結果:
調用InjectionToken構造器,options的值為undefined:
更多Jerry的原創文章,盡在:“汪子熙”:
 
總結
以上是生活随笔為你收集整理的Angular InjectionToken的一个具体使用例子的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Spring源码下载及安装(spring
- 下一篇: manifest文件使用(manifes
