向NGRX注册效果

时间:2020-04-15 08:02:45

标签: angular typescript ngrx ngrx-effects

从服务器收到AppConfigService响应后,如何在AppModule中注册CompanyEffects。

在AppModule中进行效果注册

EffectsModule.forRoot([CompanyEffects]),

CompanyEffects

export class CompanyEffects {
    constructor(
        private actions$: Actions,
        private companyService: CompanyService,
        private env: AppConfigService,
    ) {
    }
}

@Effect()
    loadTeams$: Observable<any> = this.actions$.pipe(
        ofType(companyActions.CompanyActionsTypes.ALL_TEAMS_REQUEST),
        mergeMap(() =>
            this.companyService.teamList().pipe(
             ...
            )
        ));

AppConfigService

export class AppConfigService {
    public appConfig: AppConfig;

    constructor(private injector: Injector) { }

    loadAppConfig(): Promise<any> {
        const http = this.injector.get(HttpClient);
        let configUrl = "/api/data/config";

        const promise = http
            .get(configUrl)
            .toPromise()
            .then((data: AppConfig) => {
                this.appConfig = data;
                return data;
            });
        return promise;
    }      
}

1 个答案:

答案 0 :(得分:0)

没有办法。

您只能更新CompanyEffects来跳过/延迟操作,直到出现this.env.appConfig为止。