Search code examples
angularangular5cyclic-dependency

Angular 5 APP_INITIALIZER causes Cylic Dependency


similar to Angular 5 APP_INITIALIZER gives Cyclic dependency error

I have an app which uses Angular 5 and SSR, I have just upgraded it all from Angular 4 where all was well.

Now when I use the APP_INITIALIZER I get the above mentioned error. I looked around and the best I could find was related to needing to inject Router, but I don't use Router! any Guidence on this would be great, thanks in advance!

here's the code:

in my providers, and function :-

  SettingsProvider,
  { provide: APP_INITIALIZER, useFactory: settingsProviderFactory, deps: [SettingsProvider], multi: true }

export function settingsProviderFactory(provider: SettingsProvider) {
  return () => provider.load();
}

The service:

import { HttpClient } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { Subject } from "rxjs";
import { ConfigSetting } from "../../models/config.model";


@Injectable()
export class SettingsProvider {

    private settings: any = null;
    private baseUrl: string;

    public subject: Subject<any> = new Subject();

    constructor(private http: HttpClient) {

    }

    load() {
        return new Promise((resolve, reject) => {
            this.http
              .get('http://localhost:54601/api/configuration')
                .subscribe(response => {
                    this.settings = response;
                    resolve(true);
                    console.log(response)
                })
        })
    }

    public getEventSetting(): boolean {
        return true /*this.settings['events'];*/
    }
    public getReviewSetting(): boolean {
        return true /*this.settings['reviews'];*/
    }
    public updateSettings(settings: ConfigSetting): void {
        this.settings = settings;
        this.subject.next();
    }

}

EDIT: Ok, I could be wrong but it looks like it may be to do with HttpClient. I remove that and it seems to work... sort of lol I can get a message to the console. I need http though! I tried adding a timeout and injecting it, but that didn't work.


Solution

  • The problem was resolved by using the app.browser.module rather than the shared app.module