简体   繁体   English

Angular 2服务依赖注入问题

[英]Angular 2 Services Dependency Injection Issue

In my app I have a Module with two components, ListComponent and DetailsComponent and a service MyModuleService. 在我的应用程序中,我有一个包含两个组件的模块,ListComponent和DetailsComponent以及一个服务MyModuleService。

When users visits the ListComponet, I fetch the list from the server and add it to the List property of the service (which I then use as a cache). 当用户访问ListComponet时,我从服务器获取列表并将其添加到服务的List属性(然后我将其用作缓存)。

When User clicks any item in the list, its taken to the Details page of that item. 当用户单击列表中的任何项目时,它将进入该项目的“详细信息”页面。

In details component I have navigation with Next and Previous links, I use the List property of the service to decide about the next and previous item. 在详细信息组件中,我使用Next和Previous链接进行导航,我使用服务的List属性来决定下一个和上一个项目。

If a user go back to the list page, the list is rendered from the cache ie from the List property of the service. 如果用户返回列表页面,则从缓存中呈现列表,即从服务的List属性呈现。

My List component also have a search form which allow users to specify search criteria and retrieve items from server matching that criteria. “我的列表”组件还有一个搜索表单,允许用户指定搜索条件并从符合该条件的服务器中检索项目。

The issue is, if I navigate to any other route which doesn't have any reference to above mentioned MyModule and then come back to the list page, it doesn't refresh the MyModuleService which means that it didn't destroy the Service instance. 问题是,如果我导航到任何其他没有任何对上述MyModule的引用的路由,然后回到列表页面,它不会刷新MyModuleService,这意味着它没有销毁Service实例。

Probably because the MyModuleService is added to global dependency injection context. 可能是因为MyModuleService被添加到全局依赖注入上下文中。

Can anyone please guide, how do I limit the scope of a service to a module only? 任何人都可以请指导,我如何仅限于模块的服务范围? so that when I navigate away to another route it destroys the MyModuleService instance. 所以当我导航到另一条路线时,它会破坏MyModuleService实例。

Probably because the MyModuleService is added to global dependency injection context. 可能是因为MyModuleService被添加到全局依赖注入上下文中。

Yes, and this is by design . 是的, 这是设计的

The way to restrict the scope to the module is by using a "top component" in the module (as said in the given documentation link), then declaring the service in the providers array of the @Component decorator: 将范围限制到模块的方法是使用模块中的“顶部组件”(如给定文档链接中所述),然后在@Component装饰器的providers数组中声明服务:

@Component({
    selector: 'my-top-component',
    providers: [ MyModuleService ]
})
export class MyTopComponent { }

This way, MyModuleService scope will be restricted to MyTopComponent and its children. 这样, MyModuleService范围将限制为MyTopComponent及其子级。 But since you'll have two nested components ( ListComponent and DetailsComponent ) inside MyTopComponent , you'll have to deal with child routes too. 但是,因为你有两个嵌套组件( ListComponentDetailsComponent内) MyTopComponent ,你必须处理孩子的路线了。

To provide different instances of the service to different modules use the providers array. 要为不同的模块提供不同的服务实例,请使用providers数组。

Module1:

providers: [MyService]

Module2:

providers: [MyService]

Now these two modules are using different instances of the same service. 现在这两个模块正在使用相同服务的不同实例。

You can declare a provider at a component level if you want. 如果需要,可以在组件级别声明提供程序。 That means that that component and any sub components will have instance to that individual instance of the service. 这意味着该组件和任何子组件将具有该服务的单个实例的实例。

If you want the whole app to use just once instance of a service then provide it in the providers array at the top level module. 如果您希望整个应用程序只使用一次服务实例,那么请在顶级模块的providers数组中提供它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM