非单一服务 [英] Non-singleton services

查看:50
本文介绍了非单一服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件上传组件,我在一个视图中使用了几次.我有一项服务,用于管理有关每个上传文件的元数据.当我将文件添加到一个组件时,所有组件开始更新,而不仅仅是已添加文件的组件.

I have a file upload component that I'm using several times in one view. I have a service that manages the metadata about each uploading file. When I add files to one component, all the components start updating instead of just the one that had files added.

是否可以为正在显示的每个组件附加新的服务实例?

Is there a way to have a new instance of the service attached for each component that is being displayed?

推荐答案

根据文档:

在组件级别提供服务可确保组件的每个实例都获得其自己的私有服务实例.

Providing the service at the component level ensures that every instance of the component gets its own, private instance of the service.

因此,与其将服务列在应用程序模块的 providers 中,不如尝试将其列在较低级别的组件定义中.

So rather than listing the service in the providers of the application module, try listing it in a lower-level component definition.

这篇关于非单一服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆