3

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

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

export interface ItemState extends EntityState<Item> {}

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

我收到错误:StaticInjectorError(Platform: core)[ItemsService -> ItemsStore]: NullInjectorError: No provider for ItemsStore!

它应该工作

4

2 回答 2

7

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

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

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

于 2019-10-17T09:09:43.553 回答
2

我想 ItemService 是您提供的一项服务,只是您忘记为其添加提供程序。您可以将 @Injectable() 装饰器更改为如下所示。

@Injector({
    providedIn: 'root'
})

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

它会像,

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

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

于 2019-10-17T09:15:04.043 回答