admin管理员组文章数量:1391975
I have in my ApplicationConfig provider
provideAppInitializer(() => {
inject(AppConfigService)
})
In component MainPageComponent I have ngOnInit(), which geting data from backend every refresh.
ngOnInit() {
this.loginService.getHi().subscribe({
next: result => {
this.text = result;
void this._router.navigate(['mainPage'])
},
error: err => {
void this._router.navigate(['login'])
console.log("Login failed", err);
},
complete: () => {
console.log('Login process completed ' + localStorage.getItem("token"));
}
})
problem is Global app initialization is after ngOnInit() is called, how to avoid it?
AppConfigService
export class AppConfigService {
private appConfig!: AppConfig;
constructor(private readonly http: HttpClient) {
void this.loadAppConfig();
}
loadAppConfig(): Promise<any> {
return firstValueFrom(this.http.get<AppConfig>('/config/config.json').pipe(tap(data => {
console.log('my data ' + JSON.stringify(data));
this.appConfig = data;
})));
}
getBackendBaseUrl(): string {
return this.appConfig.environment.backendURL;
}
I have in my ApplicationConfig provider
provideAppInitializer(() => {
inject(AppConfigService)
})
In component MainPageComponent I have ngOnInit(), which geting data from backend every refresh.
ngOnInit() {
this.loginService.getHi().subscribe({
next: result => {
this.text = result;
void this._router.navigate(['mainPage'])
},
error: err => {
void this._router.navigate(['login'])
console.log("Login failed", err);
},
complete: () => {
console.log('Login process completed ' + localStorage.getItem("token"));
}
})
problem is Global app initialization is after ngOnInit() is called, how to avoid it?
AppConfigService
export class AppConfigService {
private appConfig!: AppConfig;
constructor(private readonly http: HttpClient) {
void this.loadAppConfig();
}
loadAppConfig(): Promise<any> {
return firstValueFrom(this.http.get<AppConfig>('/config/config.json').pipe(tap(data => {
console.log('my data ' + JSON.stringify(data));
this.appConfig = data;
})));
}
getBackendBaseUrl(): string {
return this.appConfig.environment.backendURL;
}
Share
Improve this question
edited Mar 13 at 17:29
Naren Murali
60.4k5 gold badges44 silver badges78 bronze badges
asked Mar 13 at 17:23
WynnyWynny
515 bronze badges
1 Answer
Reset to default 2Since we are triggering the API on the constructor of the service. The app initializer will not wait for the API to complete, you must return the promise, so that once it is resolved the application loads.
So we execute and return the method inside the provideAppInitializer
, so that once the promise load is completed the application will load.
First we remove the call from the constructor.
export class AppConfigService {
private appConfig!: AppConfig;
loadAppConfig(): Promise<any> {
return firstValueFrom(this.http.get<AppConfig>('/config/config.json').pipe(tap(data => {
console.log('my data ' + JSON.stringify(data));
this.appConfig = data;
})));
}
getBackendBaseUrl(): string {
return this.appConfig.environment.backendURL;
}
Then we return the promise from execution of loadAppConfig
.
provideAppInitializer(() => {
const appConfigService = inject(AppConfigService);
return appConfigService.loadAppConfig();
})
本文标签: javascriptAngular provideAppInitializer is initialized after ngOnInitStack Overflow
版权声明:本文标题:javascript - Angular provideAppInitializer is initialized after ngOnInit - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744688066a2619824.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论