تایپاسکریپت - تزریق LoadingController در متد استاتیک در انگولار 2


من سعی کردهام LoadingController را در متد استاتیک تزریق کنم، اما تزریق نمیشود. آیا ممکن است LoadingController را در متد استاتیک تزریق کرد؟

من کد زیر را امتحان کردهام:

import { LoadingController, Loading } from 'ionic-angular';
import { Inject } from '@angular/core'

export class Commons {

    @Inject(LoadingController)
    static showLoadingMask( loadingText: string ) {
        //Getting Null
        let loadCtrl: LoadingController;
    }
}

کد دیگری که من امتحان کردهام این است:

import { LoadingController, Loading } from 'ionic-angular';
import { Inject } from '@angular/core'

export class Commons {
    static showLoadingMask( loadingText: string, @Inject(LoadingController) loadCtrl ) {
        //Getting Null
        if( loadCtrl ) {
            //Null
        } else {
            //Always executing else
        }
    }

}

من نمیخواهم سازندهای در این کلاس داشته باشم، زیرا تنها میخواهم متدهای ایستا ارائه دهم که قابل استفاده مجدد در کل پروژه برای کارهای رایج باشند.

پاسخ شماره ۱

من مطمئن نیستم که این بهترین روش برای انجام این کار باشد، اما میتوانید یک مرجع به injector ارسال کنید و از آن برای دریافت نمونههای ارائهدهندگان استفاده کنید. برای مثال

import { LoadingController, Loading } from 'ionic-angular';
import { Injector } from '@angular/core'

export class Commons {
    static showLoadingMask(injector: Injector, loadingText: string ) {
        let loadCtrl: LoadingController = this.injector.get(LoadingController);
    }
}

و در کامپوننتی که میخواهید از آن استفاده کنید، به این صورت خواهد بود

import { Component, Injector } from '@angular/core';
// ...

@Component({
    selector: 'page-home',
    templateUrl: 'home.html'
})
export class HomePage {

    constructor(..., public injector: Injector) { ... }

    public yourMethod(): void {
      Commons.showLoadingMask(this.injector, 'Loading...');
    }    
}