如何使用BehaviourSubject设置将数据传递到另一个模块组件 [英] How to set pass data to another module component using BehaviourSubject

查看:65
本文介绍了如何使用BehaviourSubject设置将数据传递到另一个模块组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了将数据从一个组件传递到另一个组件,我在项目中多次使用以下方法.但是,这次我有2个组件:parent(在AppModule中)和sidebar(在CoreModule中)组件,并且我试图在下拉更改时将数据从父级传递到侧栏.但是,它不起作用,并且侧栏组件(在示例子组件中)的 subscribe 方法不会触发,除非是从初始加载开始.是否在不同的模块上会导致此问题?或其他有关此事的问题?

I have used the following approach several times in my projects in order to pass data from one component to another. However, this time I have 2 component: parent (in AppModule) and sidebar (in CoreModule) components and I am trying to pass data from parent to sidebar on a dropdown change. However, it does not work and the subscribe method on the sidebar component (in the example child component) is not triggered except from initial loading. Does being on different modules cause this problem? Or any other issue about that?

这是 更新:

这是我的模块关系:

parent.module:

imports: [
  SharedModule
],
providers: []

child.module:

imports: [
  SharedModule
],
providers: []

共享模块.

providers: [
    DataService
]

当我如上所述更新时,它仍然不起作用.但是,如果我将providers字段中的 DataService shared.module 移到了 app.module ,它将起作用.但是我认为,仅当它位于如上所示的 shared.module 中时,它才应该工作.为什么?

When I update as shown above, it still does not work. However, if I move DataService in the providers field from shared.module to app.module it works. But I think it should work when it is only in shared.module as shown above. Why?

推荐答案

我认为问题与每个模块中的提供程序实例有关,您是否尝试过仅将1个提供程序添加到顶部父模块并查看结果?>

I believe the issue is related to the provider instance in each module, have you tried adding only 1 provider to the top parent module and see the result?

使用组件与其他模块的服务有一个重要的区别使用另一个模块的组件和使用一个组件之间的区别来自另一个模块的服务.想要使用时导入模块指令,管道和组件.导入包含服务的模块表示您将拥有该服务的新实例,通常不是您所需要的(通常是想重用现有服务).使用模块导入来控制服务实例化.

Using components vs services from other modules There is an important distinction between using another module's component and using a service from another module. Import modules when you want to use directives, pipes, and components. Importing a module with services means that you will have a new instance of that service, which typically is not what you need (typically one wants to reuse an existing service). Use module imports to control service instantiation.

获得共享服务的最常见方法是通过角度依赖注入,而不是通过模块系统(导入模块将产生一个新的服务实例,即不是典型的用法).

The most common way to get a hold of shared services is through Angular dependency injection, rather than through the module system (importing a module will result in a new service instance, which is not a typical usage).

要了解有关共享服务的信息,请参阅提供商. https://angular.io/guide/sharing-ngmodules

To read about sharing services, see Providers. https://angular.io/guide/sharing-ngmodules

基本上,当您导入一个新模块时,每次导入都会创建一个提供者的新实例,因此注入是不同的,如果您需要两者的数据相同,则需要在AppModule中处理提供者.这种情况

Basically, when you import a new module, a new instance of provider is created by each import, so the injection is different, if you need the data to be same for both, you need to handle the provider in the AppModule in this case

这篇关于如何使用BehaviourSubject设置将数据传递到另一个模块组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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