Akita and Angular Error: StaticInjector and NullInjector Store, Query

Akita and Angular Error: StaticInjector and NullInjector Store, Query

我在 Angular 项目上实施秋田状态管理时遇到了错误。我只是提供一个简短的答案,以便像我这样的人可以解决这个问题。

Akita 文档和示例中对此没有明确的理解。

export interface ItemState extends EntityState<Item> {}

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

我收到错误: StaticInjectorError(平台:核心)[ItemsService -> ItemsStore]: NullInjectorError:没有 ItemsStore 的提供者!

它应该有效

文档中没有提到它,但为了让它起作用,我们只需要添加 provideIn: 'root'

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

ItemsQuery 也是如此。希望这对某人有帮助

我想 ItemService 是你做的服务,只是你忘了为它添加一个提供者。您可以将 @Injectable() 装饰器更改为下面的方式。

@Injector({
    providedIn: 'root'
})

或者您可以将服务作为提供者添加到您正在使用该服务的模块中。

就像,

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

不要忘记在模块中导入服务。