Setting locale for Datepipe in Angular 16 standalone Components fails at build

huangapple go评论43阅读模式
英文:

Setting locale for Datepipe in Angular 16 standalone Components fails at build

问题

Here is the translated code section:

当在本地构建时,它正常工作。
当在Gitlab Pipeline中构建时,我遇到了这个错误:

./src/app/app.component.ts:6:0-55 - 错误: 未找到模块: 错误: 无法解析 '@angular/common/locales/de-at' 在 '/usr/src/app/copacking-frontend/src/app' 中
错误: src/app/app.component.ts:11:24 - 错误 TS2307: 无法找到模块 '@angular/common/locales/de-at' 或其对应的类型声明。
11 import localeDeAt from '@angular/common/locales/de-at';


我的依赖关系如下:
"@angular/animations": "^16.0.1",
"@angular/common": "^16.0.1",
"@angular/compiler": "^16.0.1",
"@angular/core": "^16.0.1",
"@angular/forms": "^16.0.1",
"@angular/platform-browser": "^16.0.1",
"@angular/platform-browser-dynamic": "^16.0.1",
"@angular/router": "^16.0.1",
"@ngrx/effects": "^16.0.0",
"@ngrx/entity": "^16.0.0",
"@ngrx/store": "^16.0.0",
"@ngrx/store-devtools": "^16.0.0",
"angular-calendar": "^0.31.0",
"chart.js": "^4.2.1",
"date-fns": "^2.30.0",
"jwt-decode": "^3.1.2",
"keyboard-css": "^1.2.4",
"primeicons": "^6.0.1",
"primeng": "^16.0.0-rc.1",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"xlsx": "^0.18.5",
"zone.js": "~0.13.0"

在我的 main.ts 中的 bootstrapApplication:

import { bootstrapApplication } from "@angular/platform-browser";
import { provideRouter } from "@angular/router";
import { routes } from "./app/app-routing.module";
import { AppComponent } from "./app/app.component";
import { LOCALE_ID, importProvidersFrom, isDevMode } from "@angular/core";
import { provideEffects } from "@ngrx/effects";
import { provideStoreDevtools } from "@ngrx/store-devtools";
import { AuthEffects } from "./app/services/auth/state/auth.effects";
import { StandortEffects } from "./app/shared/state/standorte/standort.effects";
import { AuftragEffects } from "./app/views/auftraege/state/auftraege.effects";
import { ArtikelEffects } from "./app/views/stammdaten/artikel/state/artikel.effects";
import { UnternehmenEffects } from "./app/views/stammdaten/unternehmen/state/unternehmen.effects";
import { provideStore } from "@ngrx/store";
import { authReducer } from "./app/services/auth/state/auth.reducers";
import { standortReducer } from "./app/shared/state/standorte/standort.reducers";
import { auftraegeReducer } from "./app/views/auftraege/state/auftraege.reducers";
import { artikelReducer } from "./app/views/stammdaten/artikel/state/artikel.reducers";
import { unternehemReducer } from "./app/views/stammdaten/unternehmen/state/unternehmen.reducers";
import { HTTP_INTERCEPTORS, HttpClientModule } from "@angular/common/http";
import { AuthInterceptor } from "./app/services/auth/auth.interceptor";
import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
import { ConfirmationService } from "primeng/api";
import { CalendarModule, DateAdapter} from "angular-calendar";
import { adapterFactory } from 'angular-calendar/date-adapters/date-fns';

bootstrapApplication(AppComponent, {providers: [
provideRouter(routes),
provideEffects([AuthEffects, UnternehmenEffects, StandortEffects, ArtikelEffects, AuftragEffects]),
provideStore( {
auth: authReducer,
standorte: standortReducer,
unternehmen: unternehemReducer,
artikel: artikelReducer,
auftraege: auftraegeReducer
}),
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true
},
ConfirmationService,
{ provide: LOCALE_ID, useValue: "de-AT" },
provideStoreDevtools({ maxAge: 25, logOnly: isDevMode() }),
importProvidersFrom([
BrowserAnimationsModule,
HttpClientModule,
CalendarModule.forRoot({
provide: DateAdapter,
useFactory: adapterFactory,
}),
])
]});


在我的 AppComponent 中,我在 ngOnInit 中进行了注册:

import localeDeAt from '@angular/common/locales/de-at';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
standalone: true,
imports: [
RouterOutlet,
SidenavComponent,
CommonModule
]
})
export class AppComponent implements OnInit{
ngOnInit() {
registerLocaleData(localeDeAt);
}
...
}


我尝试了一些方法,但没有真正导致任何差异。请问有谁能帮助我解决这个问题?

希望这能帮助您解决问题。如果有其他问题,请随时提出。

英文:

When building locally it works fine.
When building in the Gitlab Pipeline I encounter this error:

./src/app/app.component.ts:6:0-55 - Error: Module not found: Error: Can't resolve '@angular/common/locales/de-at' in '/usr/src/app/copacking-frontend/src/app'
Error: src/app/app.component.ts:11:24 - error TS2307: Cannot find module '@angular/common/locales/de-at' or its corresponding type declarations.
11 import localeDeAt from '@angular/common/locales/de-at';

I have this deps:
"@angular/animations": "^16.0.1",
"@angular/common": "^16.0.1",
"@angular/compiler": "^16.0.1",
"@angular/core": "^16.0.1",
"@angular/forms": "^16.0.1",
"@angular/platform-browser": "^16.0.1",
"@angular/platform-browser-dynamic": "^16.0.1",
"@angular/router": "^16.0.1",
"@ngrx/effects": "^16.0.0",
"@ngrx/entity": "^16.0.0",
"@ngrx/store": "^16.0.0",
"@ngrx/store-devtools": "^16.0.0",
"angular-calendar": "^0.31.0",
"chart.js": "^4.2.1",
"date-fns": "^2.30.0",
"jwt-decode": "^3.1.2",
"keyboard-css": "^1.2.4",
"primeicons": "^6.0.1",
"primeng": "^16.0.0-rc.1",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"xlsx": "^0.18.5",
"zone.js": "~0.13.0"

In my bootstrapApplication in main.ts

import { bootstrapApplication } from "@angular/platform-browser";
import { provideRouter } from "@angular/router";
import { routes } from "./app/app-routing.module";
import { AppComponent } from "./app/app.component";
import { LOCALE_ID, importProvidersFrom, isDevMode } from "@angular/core";
import { provideEffects } from "@ngrx/effects";
import { provideStoreDevtools } from "@ngrx/store-devtools";
import { AuthEffects } from "./app/services/auth/state/auth.effects";
import { StandortEffects } from "./app/shared/state/standorte/standort.effects";
import { AuftragEffects } from "./app/views/auftraege/state/auftraege.effects";
import { ArtikelEffects } from "./app/views/stammdaten/artikel/state/artikel.effects";
import { UnternehmenEffects } from "./app/views/stammdaten/unternehmen/state/unternehmen.effects";
import { provideStore } from "@ngrx/store";
import { authReducer } from "./app/services/auth/state/auth.reducers";
import { standortReducer } from "./app/shared/state/standorte/standort.reducers";
import { auftraegeReducer } from "./app/views/auftraege/state/auftraege.reducers";
import { artikelReducer } from "./app/views/stammdaten/artikel/state/artikel.reducers";
import { unternehemReducer } from "./app/views/stammdaten/unternehmen/state/unternehmen.reducers";
import { HTTP_INTERCEPTORS, HttpClientModule } from "@angular/common/http";
import { AuthInterceptor } from "./app/services/auth/auth.interceptor";
import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
import { ConfirmationService } from "primeng/api";
import { CalendarModule, DateAdapter} from "angular-calendar";
import { adapterFactory } from 'angular-calendar/date-adapters/date-fns';

bootstrapApplication(AppComponent, {providers: [
  provideRouter(routes),
  provideEffects([AuthEffects, UnternehmenEffects, StandortEffects, ArtikelEffects, AuftragEffects]),
  provideStore( { 
    auth: authReducer,
    standorte: standortReducer,
    unternehmen: unternehemReducer,
    artikel: artikelReducer,
    auftraege: auftraegeReducer
  }),
  {
    provide: HTTP_INTERCEPTORS,
    useClass: AuthInterceptor,
    multi: true
  },
  ConfirmationService,
  { provide: LOCALE_ID, useValue: "de-AT" },
  provideStoreDevtools({ maxAge: 25, logOnly: isDevMode() }),
  importProvidersFrom([
    BrowserAnimationsModule,
    HttpClientModule, 
    CalendarModule.forRoot({
      provide: DateAdapter,
      useFactory: adapterFactory,
    }),
  ])
]});

And in my AppComponent I register it in ngOnInit:

import localeDeAt from '@angular/common/locales/de-at';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
  standalone: true,
  imports: [
    RouterOutlet,
    SidenavComponent,
    CommonModule
  ]
})
export class AppComponent implements OnInit{
ngOnInit() {
    registerLocaleData(localeDeAt);
}
...
}

I tried some things but nothing really lead to any difference.

Could someone enlighten me?

答案1

得分: 2

"Thanks to you I was able to make my own locale in Angular work. And I think I have found the solution to your problem at the same time.

Try to put registerLocaleData() inside of the AppComponent constructor, instead of trying to implement NgOnInit

Hope it will help."

英文:

Thanks to you I was able to make my own locale in Angular work. And I think I have found the solution to your problem at the same time.

Try to put registerLocaleData() inside of the AppComponent constructor, instead of trying to implement NgOnInit

Hope it will help

huangapple
  • 本文由 发表于 2023年5月13日 17:06:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定