问题
Angular 5 error. I am having trouble with getting user details on page refresh from browser. I am trying to load the data using APP_INITIALIZER but getting below error -
compiler.js:19390 Uncaught Error: Provider parse errors: Cannot instantiate cyclic dependency! ApplicationRef ("[ERROR ->]"): in NgModule AppModule in ./AppModule@-1:-1
Below is my configuration app.module.ts
export function initUserFactory(userService: UserService) {
return () => { userService.initUser()};
}
let INITIAL_PROVIDERS = [ {provide: HTTP_INTERCEPTORS,useClass: RequestAuthInterceptor, multi: true },
UserService,
MessageService,
AuthGuard,
AdminAuthGuard,
NotificationService,
NgxPermissionsService,
{
provide: APP_INITIALIZER,
useFactory: initUserFactory,
deps: [UserService],
multi: true
},
AuthService,
];
@NgModule({ imports:[//required imports],declarations: INITIAL_COMPONENTS,
providers: INITIAL_PROVIDERS,
bootstrap: [AppComponent]
})
UserService
initUser() {
const promise = this.http.get<APIResponse>(this.apiurl+ServiceConstants.REFRESH_TOKEN).toPromise()
.then(response => {
if(response.data.result != null && response.data.result.token != null ){
//refresh toekn
let accessToken = response.data.result.token;
localStorage.setItem('auth_token', accessToken);
return this.getUserInfo().toPromise()
.then(user => {
this.currentUser = user;
});
}
})
.catch(() => null);
return promise;
}
Or any other suggestions where I can resolve logged in user before rending the view! Because the user is not resolved the auth guard redirect it to login page on page refresh. Any help is appreciated! Thanks in advance
来源:https://stackoverflow.com/questions/48329154/angular-5-app-initializer-gives-cyclic-dependency-error