Last active
December 14, 2021 12:53
-
-
Save peplocanto/2ecc58ba9c0ed09dd9de035a012fb88b to your computer and use it in GitHub Desktop.
How to start services in core module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// core.providers.ts | |
import { APP_INITIALIZER } from '@angular/core'; | |
export const load<domain>ServiceFnFactory = (<domain>Service: <domain>Service): (() => Promise<any>) => () => { | |
<domain>Service.init(); | |
return Promise.resolve(); | |
}; | |
export const APP_INITIALIZER_PROVIDERS: Provider[] = [ | |
{ | |
provide: APP_INITIALIZER, | |
useFactory: load<domain>ServiceFnFactory, | |
deps: [<domain>Service], | |
multi: true | |
} | |
]; | |
// core.module.ts | |
import { APP_INITIALIZER_PROVIDERS } from 'app-core/core.providers'; | |
@NgModule({ | |
imports: [...MODULES], | |
declarations: [...INNER_COMPONENTS, ...OUTER_COMPONENTS], | |
exports: [...OUTER_COMPONENTS], | |
providers: [...APP_INITIALIZER_PROVIDERS] | |
}) | |
export class CoreModule { | |
constructor(@Optional() @SkipSelf() parentModule: CoreModule) { | |
if (!!parentModule) { | |
console.error('"CoreModule" already loaded. Import it _ONLY_ on main "AppRootModule".'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment