0

I am using ngxs 3.7.5 with Angular 14

// single slice 
@State<EnvironmentStateModel>({
  name: 'environment',
  defaults: {
    productionBuild: environment.production,
    internalPlatform: detectInternalPlatform(window.location.hostname, window.location.port),
    platform: detectPlattform(),
    appVersion: environment.appVersion   
  }
})
@Injectable()
export class EnvironmentState {
  
}

I am injecting the store into a HttpInterceptor

export class MessageHeaderInterceptor implements HttpInterceptor {


  constructor(private userService: UserService, private store: Store) {
    
    console.log('constructor', this.store.selectSnapshot((appState: AppState) => appState));
  }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    
    console.log('intercept', this.store.selectSnapshot((appState: AppState) => appState));

    //...
  
  }

}

The console statement in the constructor shows {}, also the console statement in the first call to intercept. However consecutive calls to intercept show { environment: ... } that is with the environment slice properly initialized. I expect the store to be properly initialized when using it the first time. What am I doing wrong?

pesche666
  • 139
  • 1
  • 13
  • Found the problem: HttpInterceptor was called when loading i18n JSON resource files, this happens at a very early stage in Angular application start. I dont need store for these type of http requests and filter them out. – pesche666 Aug 17 '22 at 08:38

1 Answers1

0

When you include the NgxsModule in the import, the EnvironmentState should be passed to the NgxsModule's .forRoot() function (if it's a root store), or .forFeature() function (if it's a lazy-loaded store).

You can find more details here:

Amer
  • 6,162
  • 2
  • 8
  • 34