admin管理员组文章数量:1332345
I am trying to upgrade from Angular 18 to 19. Automatic migration changed the following code
providers: [
{
provide: APP_INITIALIZER,
useFactory: initializeApp1,
deps: [AuthService],
multi: true
},
]
to this:
providers: [
provideAppInitializer(initializeApp1(inject(AuthService)))
]
however, when I run the code, I am getting this error
Uncaught RuntimeError: NG0203: inject() must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with
runInInjectionContext
. Find more at /errors/NG0203`
I am pretty stuck as to how to solve this error, could you help me?
I am trying to upgrade from Angular 18 to 19. Automatic migration changed the following code
providers: [
{
provide: APP_INITIALIZER,
useFactory: initializeApp1,
deps: [AuthService],
multi: true
},
]
to this:
providers: [
provideAppInitializer(initializeApp1(inject(AuthService)))
]
however, when I run the code, I am getting this error
Uncaught RuntimeError: NG0203: inject() must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with
runInInjectionContext
. Find more at https://angular.dev/errors/NG0203`
I am pretty stuck as to how to solve this error, could you help me?
Share Improve this question edited Nov 21, 2024 at 19:59 Chait 1,3252 gold badges22 silver badges37 bronze badges asked Nov 20, 2024 at 21:14 IgorIgor 8142 gold badges13 silver badges37 bronze badges 1- This is actually a bug in the migration itself. A fix is pending : github/angular/angular/pull/58518 – Matthieu Riegler Commented Nov 20, 2024 at 23:54
3 Answers
Reset to default 13You'll need wrap the initializer in an arrow function to be in the injection context:
provideAppInitializer(() => intializeApp1(inject(AuthService)))
During ng update @angular/core@19 @angular/cli@19
you'll see this message:
** Optional migrations of package '@angular/core' **
This package has 1 optional migration that can be executed.
❯ Replaces `APP_INITIALIZER`, `ENVIRONMENT_INITIALIZER` &
`PLATFORM_INITIALIZER` respectively with `provideAppInitializer`,
`provideEnvironmentInitializer` & `providePlatformInitializer`.
ng update @angular/core --name provide-initializer
Just do what is says and run ng update @angular/core --name provide-initializer
and it should remove APP_INITIALIZER
for you and replace it with the correct code.
Had the same issue: The bellow resolved it for me.
provideAppInitializer(()=> {
const initFn = ((key: KeycloakService) => {
return () => key.init({
config: {
url: 'sso_url',
realm: 'realm',
clientId: 'client_id'
},
initOptions: {
onLoad: 'check-sso',
silentCheckSsoRedirectUri: window.location.origin + '/assets/silent-check-sso.html'
}})
})(inject(KeycloakService));
return initFn();
}),
本文标签: angular19angular 19APPINITIALIZER deprecationStack Overflow
版权声明:本文标题:angular19 - angular 19 - APP_INITIALIZER deprecation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742327871a2454087.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论