Angular 5 APP_INITIALIZER给出循环依赖性错误

时间:2018-01-18 19:44:08

标签: angular2-routing angular2-services angular5 cyclic-dependency

Angular 5错误。 我无法从浏览器获取页面刷新的用户详细信息。我正在尝试使用APP_INITIALIZER加载数据但是低于错误 -

compiler.js:19390未捕获错误:提供程序解析错误: 无法实例化循环依赖! ApplicationRef(“[ERROR - >]”):在./AppModule@-1:1 / / strong>中的NgModule AppModule中

以下是我的配置 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;
  }

或任何其他建议,我可以在退出视图之前解决登录用户! 由于用户未解析,因此auth guard会在页面刷新时将其重定向到登录页面。 任何帮助表示赞赏! 提前致谢

0 个答案:

没有答案