app.module.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { NgModule, LOCALE_ID, APP_INITIALIZER } from '@angular/core';
  2. import { HttpClient, HttpClientModule } from '@angular/common/http';
  3. import { BrowserModule } from '@angular/platform-browser';
  4. import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
  5. // #region default language
  6. // 参考:https://ng-alain.com/docs/i18n
  7. import { default as ngLang } from '@angular/common/locales/zh';
  8. import { NZ_I18N, zh_CN as zorroLang } from 'ng-zorro-antd';
  9. import { DELON_LOCALE, zh_CN as delonLang } from '@delon/theme';
  10. const LANG = {
  11. abbr: 'zh',
  12. ng: ngLang,
  13. zorro: zorroLang,
  14. delon: delonLang,
  15. };
  16. // register angular
  17. import { registerLocaleData } from '@angular/common';
  18. registerLocaleData(LANG.ng, LANG.abbr);
  19. const LANG_PROVIDES = [
  20. { provide: LOCALE_ID, useValue: LANG.abbr },
  21. { provide: NZ_I18N, useValue: LANG.zorro },
  22. { provide: DELON_LOCALE, useValue: LANG.delon },
  23. ];
  24. // #endregion
  25. // #region i18n services
  26. import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
  27. import { TranslateHttpLoader } from '@ngx-translate/http-loader';
  28. import { ALAIN_I18N_TOKEN } from '@delon/theme';
  29. import { I18NService } from '@core';
  30. // 加载i18n语言文件
  31. export function I18nHttpLoaderFactory(http: HttpClient) {
  32. return new TranslateHttpLoader(http, `assets/tmp/i18n/`, '.json');
  33. }
  34. const I18NSERVICE_MODULES = [
  35. TranslateModule.forRoot({
  36. loader: {
  37. provide: TranslateLoader,
  38. useFactory: I18nHttpLoaderFactory,
  39. deps: [HttpClient],
  40. },
  41. }),
  42. ];
  43. const I18NSERVICE_PROVIDES = [{ provide: ALAIN_I18N_TOKEN, useClass: I18NService, multi: false }];
  44. // #endregion
  45. // #region global third module
  46. const GLOBAL_THIRD_MODULES = [];
  47. // #endregion
  48. // #region JSON Schema form (using @delon/form)
  49. import { JsonSchemaModule } from '@shared/json-schema/json-schema.module';
  50. const FORM_MODULES = [JsonSchemaModule];
  51. // #endregion
  52. // #region Http Interceptors
  53. import { HTTP_INTERCEPTORS } from '@angular/common/http';
  54. import { SimpleInterceptor } from '@delon/auth';
  55. import { DefaultInterceptor } from '@core';
  56. const INTERCEPTOR_PROVIDES = [
  57. { provide: HTTP_INTERCEPTORS, useClass: SimpleInterceptor, multi: true },
  58. { provide: HTTP_INTERCEPTORS, useClass: DefaultInterceptor, multi: true },
  59. ];
  60. // #endregion
  61. // #region Startup Service
  62. import { StartupService } from '@core';
  63. export function StartupServiceFactory(
  64. startupService: StartupService,
  65. ): Function {
  66. return () => startupService.load();
  67. }
  68. const APPINIT_PROVIDES = [
  69. StartupService,
  70. {
  71. provide: APP_INITIALIZER,
  72. useFactory: StartupServiceFactory,
  73. deps: [StartupService],
  74. multi: true,
  75. },
  76. ];
  77. // #endregion
  78. import { DelonModule } from './delon.module';
  79. import { CoreModule } from './core/core.module';
  80. import { SharedModule } from './shared/shared.module';
  81. import { RoutesModule } from './routes/routes.module';
  82. import { LayoutModule } from './layout/layout.module';
  83. import { AppComponent } from './app.component';
  84. import { RouteguardService } from '@core/guard/routeguard.service';
  85. @NgModule({
  86. declarations: [AppComponent],
  87. imports: [
  88. BrowserModule,
  89. BrowserAnimationsModule,
  90. HttpClientModule,
  91. DelonModule.forRoot(),
  92. CoreModule,
  93. SharedModule,
  94. LayoutModule,
  95. RoutesModule,
  96. ...I18NSERVICE_MODULES,
  97. ...GLOBAL_THIRD_MODULES,
  98. ...FORM_MODULES,
  99. ],
  100. providers: [...LANG_PROVIDES, ...INTERCEPTOR_PROVIDES, ...I18NSERVICE_PROVIDES, ...APPINIT_PROVIDES, RouteguardService],
  101. bootstrap: [AppComponent],
  102. })
  103. export class AppModule { }