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

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

问题描述

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

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 配置核心服务中对此进行了说明,但是该部分没有不会解释延迟加载问题.在共享模块的末尾带有一些警告的解释

请勿在共享模块中指定应用程序范围内的单例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() {
    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只是试图将服务添加到app模块和延迟加载的子模块中的@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天全站免登陆