我有一个文件上传组件,我在一个 View 中多次使用它。我有一项服务可以管理每个上传文件的元数据。当我将文件添加到一个组件时,所有组件都会开始更新,而不仅仅是添加了文件的组件。
有没有办法为正在显示的每个组件附加一个新的服务实例?
最佳答案
根据 the docs :
Providing the service at the component level ensures that every instance of the component gets its own, private instance of the service.
因此,与其将服务列在应用程序模块的 providers
中,不如尝试将其列在较低级别的组件定义中。
关于angular - 非单例服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43282546/