且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Angular 6 创建单例服务

更新时间:2023-11-13 13:58:22

这是将 providedIn 添加到服务级别的默认行为.根据 Docs

That's the default behaviour of adding providedIn to the service level. As per Docs

providedIn 这里告诉 Angular 根注入器负责用于创建 HeroService 的实例.服务是只要这种方式自动提供给整个应用程序,不需要在任何模块中列出.

providedIn here tells Angular that the root injector is responsible for creating an instance of the HeroService. Services that are provided this way are automatically made available to the entire application and don't need to be listed in any module.

在您的情况下,只需删除 providedIn: 'root' 并且仅在 module.ts 下提供.请参阅以下答案.

in your case just remove the providedIn: 'root' and have only under provides in the module.ts. Refer the following answer.