延迟加载的模块可以共享其父模块提供的相同服务实例吗? [英] Can lazy-loaded modules share the same instance of a service provided by their parent?

查看:24
本文介绍了延迟加载的模块可以共享其父模块提供的相同服务实例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚遇到了一个延迟加载模块的问题,其中父模块和子模块都需要相同的服务,但每个模块都创建了一个实例.两者的声明是相同的,即

I've just run into a problem with a lazy-loaded module where parent and child module both require the same service but create an instance each. The declaration is identical for both, that is

import { MyService } from './my.service';
...
@NgModule({
   ...
   providers: [
      MyService,
      ...
   ]
});

这里是路由设置

export parentRoutes: Routes = [
   { path: ':id', component: ParentComponent, children: [
      { path: '', component: ParentDetailsComponent },
      { path: 'child', loadChildren: 'app/child.module#ChildModule' },
      ...
   ]}
];

当然,然后在父模块中作为

which, of course, is then imported in the parent module as

RouterModule.forChild(parentRoutes)

如果我想共享同一个服务实例,我该怎么做?

How do I go about this if I wanted to share the same service instance?

推荐答案

使用 forRoot,如前所述 这里,可能是您所需要的.它要解决的问题与您在延迟加载模块获得自己的服务时遇到的问题直接相关.

Using a forRoot, as mentioned here, is what you need probably. The problem it's meant to solve, is directly related to the problem you are experiencing with lazy loaded modules getting their own service.

使用forRoot配置核心服务中对此进行了解释,但该部分没有解释延迟加载问题.这在 Shared模块

It's explained here in Configure core services with forRoot, but that section doesn't explain about the lazy-loading issue. That is explained with a little warning at the end of Shared Modules

不要在共享模块中指定应用程序范围的单例providers.导入共享模块的延迟加载模块将创建自己的服务副本.

Do not specify app-wide singleton providers in a shared module. A lazy loaded module that imports that shared module will make its own copy of the service.

@NgModule({})
class SharedModule {
  static forRoot() {
    return {
      ngModule: SharedModule,
      providers: [ MyService ]
    };
  }
}

@NgModule({
  import: [ SharedModule.forRoot() ]
})
class AppModule {}

@NgModule({
  imports: [ SharedModule ]
})
class LazyLoadedModule {}

这可以确保延迟加载的模块不会获得服务.但是无论模块是否延迟加载,这是推荐用于应用程序范围服务的模式.不过需要注意的是,如果你没有任何延迟加载的模块,不使用 forRoot 模式,而只是导入 SharedModule,它只会是服务.但仍应推荐遵循这种模式.

This makes sure that the lazy loaded module doesn't get the service. But whether or not the module is lazy loaded or not, this is the pattern that is recommended for app-wide services. Though it should be noted that if you don't have any lazy loaded module, not using the forRoot patter, and just importing SharedModule, it will only be one instance of the service. But this pattern should still recommended to be followed.

我想我在没有完全看问题的情况下就快速回答了.在问题中, 没有提到任何共享模块.似乎 OP 只是试图将服务添加到应用模块和延迟加载的子模块中的 @NgModule.providers.

I guess I jumped to quick on answering without fully looking at the question. In the question, there is no mention of any shared module. It seems the OP is simply trying to add the service to the @NgModule.providers in both the app module and the lazy loaded child module.

在这种情况下,只需从子模块 providers 中删除服务.不需要.app模块里加的那个就够孩子用了.

In this case, simply remove the service from the child module providers. It is not needed. The one added in the app module is enough for the child to be used.

请记住,providers 是应用范围的(除了本文所讨论的问题案例),而 declarations 不是.

Just remember that providers are app wide (except in the problem case this post is about), while declarations are not.

这篇关于延迟加载的模块可以共享其父模块提供的相同服务实例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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