Angular 5 APP_INITIALIZER gives Cyclic dependency error

我的梦境 提交于 2019-12-11 15:25:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!