Angular Service中的providedIn属性不起作用

问题描述 投票:-1回答:2

我正在学习Angular DI,但是提供的示例(https://angular.io/guide/providers#providedin-and-ngmodules)无法正常工作,任何人都可以帮助我了解这里是我正在研究的S​​tackblitz的链接。https://stackblitz.com/edit/angular-ivy-k1gvth谢谢,Keshav

angular angular-services injectable
2个回答
0
投票

您需要按照文档中的指定将AppService添加到app.module.ts中的providers数组。


0
投票

您可以将ProvideIn值用作root。这样就无需在应用程序模块中提供。

 providedIn: 'root'

如果要在app.module.ts中添加它,然后从服务中删除Injectable装饰器,然后添加app.module.ts的提供者列表。

您可以阅读官方文档以了解更多详细信息。 https://angular.io/guide/singleton-services

© www.soinside.com 2019 - 2024. All rights reserved.