3

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

Yevheniy Potupa
  • 537
  • 5
  • 12

2 Answers2

7

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

Yevheniy Potupa
  • 537
  • 5
  • 12
  • 1
    You are a lifesaver sir! Spend ages on this, going through docs, etc - could not find anything of relevance. Now that I saw this answer, it is in their sample code, just hard to see: https://stackblitz.com/edit/akita-todos-app?file=src%2Fapp%2Ftodos%2Fstate%2Ftodos.store.ts – Web Dev Oct 21 '19 at 12:06
  • 1
    This should be marked as the answer by the way as it does answer the question directly – Web Dev Oct 21 '19 at 12:20
2

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.

Anzal Khan
  • 61
  • 6
  • I'm not talking about service, but more for Store and Query. And if you used Injectable no need to add it to ngModule providers. You will have a lot of stores and queries it would be a mess to have all this in providers. – Yevheniy Potupa Oct 18 '19 at 10:43
  • Whatever you are talking about, the error message you given occurs when a service is neither provided in root nor a provider in some module. – Anzal Khan Oct 18 '19 at 10:47