Akita and Angular Error: StaticInjector and NullInjector Store, Query

Viewed 794

I got into an error with Akita state management implementation on the Angular project. I will just provide a short answer in order for some people like me could resolve this issue.

There is no clear understanding of this in Akita docs and examples.

export interface ItemState extends EntityState<Item> {}

@StoreConfig({ name: 'items' })
export class ItemsStore extends EntityStore<ItemState> {
  constructor() {
    super();
  }
}

I receive error: StaticInjectorError(Platform: core)[ItemsService -> ItemsStore]: NullInjectorError: No provider for ItemsStore!

It should work

2 Answers

It is not mentioned in docs, but in order for this to work we just need to add provideIn: 'root'

export interface ItemState extends EntityState<Item> {}
@Injectable({
  providedIn: 'root'
})
@StoreConfig({ name: 'items' })
export class ItemsStore extends EntityStore<ItemState> {
  constructor() {
    super();
  }
}

Same is for ItemsQuery. Hope this was helpful for somebody

I suppose ItemService is a service made by you, nad you just forgot to add a provider for the same. either you can change the @Injectable() decorator to so goven below.

@Injector({
    providedIn: 'root'
})

or you can add the service as a provider to the module that you are using the service inside.

it'll be like,

@ngModule({
...,
providers: [ItemService,...],
...
})

Don't forget to import the service in the module.

Related