Lazy loading with a common provider

I implemented a lazy loading pattern on ionic 3project.It works fine. I have one question. Let's say I need to implement my own functions StatusBarin an application. I need to implement it on each page differently, I put it on app.module.ts, as shown below.

providers: [
    StatusBar,
]

Is this a good approach, or do I need to introduce StatusBarlike provideron every component module? To me this seems like overkill. But what is the best way to do this and why?

+4
source share
2 answers

, @SrAxi , Cordova Ionic, ( ), app.module.ts .

cordova ( , )

+2

StatusBar AppModule providers, , . , StatusBar - Service. ( ).

, I18nModule ( ), . I18nModule AppModule imports, I18nModule.

forRoot():

export class I18nModule {
    static forRoot() {
        return {
            ngModule: I18nModule,
            providers: [I18nService, MyService1, MyService2]
        };
    }
}

AppModule:

imports: [
   I18nModule.forRoot(),
]

forRoot() I18nModule ( , ) singleton.

:

, , , , , 2 , , "" .

Update:

Angular , , :

imports: [
        BrowserModule,
        BrowserAnimationsModule,
        FormsModule,
        HttpModule,
        BrowserAnimationsModule,

        CoreModule,
        LayoutModule,
        I18nModule.forRoot(),
        BarHandlingModule, // Example
        InventedModule  // Example too
    ],

, Angular docs Core, , Orders, .. , .

, AppModule imports AppModule.

+1

Source: https://habr.com/ru/post/1680505/


All Articles