Primeng 动态对话的多个服务相互干扰
Multiple services with Primeng dynamic dialogs interfere with each other
我正在努力解决 angular/primeng 问题。我在不同的模块中有两个服务,每个服务都使用 DialogService 创建一个 DynamicDialog 组件。
我了解到,如果您同时拥有多个模态框,则需要单独的 DialogService 实例。我试图通过将每个服务放在它们自己的模块中并将 DialogService 放在每个模块的 "providers" 部分中来实现这一点。
每个模态框本身都可以正常工作,但我遇到了两个模态框同时显示的情况。
在这种情况下,在我关闭第二个模式后,我无法关闭第一个模式,即使关闭代码被触发并且结果为 returns。
这个问题的一个例子的 stackblitz 是:
https://stackblitz.com/github/thomasesh/twomodalproblem
如有任何帮助,我们将不胜感激。
原因是angularDI。您在 app-module 中导入 module-a 和 module-b。他们都提供 DialogService。由于 DI 的工作方式,创建了服务的单个实例。这是 imo primeng 错误,但要解决它,您可以创建一个令牌并使用令牌提供(并注入)服务的第二个实例,而不是使用相同的服务实例
// token.ts
export const DialogServiceToken = new InjectionToken('DialogService');
//module-b.ts
providers: [
{ provide: DialogServiceToken, useClass: DialogService} // this will register second instance of DialogService
]
//module-b.service
constructor( @Inject(dialogServiceToken) private dialogService: DialogService) {} // here we inject the second instance of a service
Andrei in 很好地解释了 primeng 中的“错误”,但是对我来说,向打开第二个动态对话框的组件中的提供程序添加一个新的 DialogService
就足够了
@Component({
templateUrl: './....component.html',
providers: [DialogService],
})
我正在努力解决 angular/primeng 问题。我在不同的模块中有两个服务,每个服务都使用 DialogService 创建一个 DynamicDialog 组件。
我了解到,如果您同时拥有多个模态框,则需要单独的 DialogService 实例。我试图通过将每个服务放在它们自己的模块中并将 DialogService 放在每个模块的 "providers" 部分中来实现这一点。
每个模态框本身都可以正常工作,但我遇到了两个模态框同时显示的情况。 在这种情况下,在我关闭第二个模式后,我无法关闭第一个模式,即使关闭代码被触发并且结果为 returns。
这个问题的一个例子的 stackblitz 是:
https://stackblitz.com/github/thomasesh/twomodalproblem
如有任何帮助,我们将不胜感激。
原因是angularDI。您在 app-module 中导入 module-a 和 module-b。他们都提供 DialogService。由于 DI 的工作方式,创建了服务的单个实例。这是 imo primeng 错误,但要解决它,您可以创建一个令牌并使用令牌提供(并注入)服务的第二个实例,而不是使用相同的服务实例
// token.ts
export const DialogServiceToken = new InjectionToken('DialogService');
//module-b.ts
providers: [
{ provide: DialogServiceToken, useClass: DialogService} // this will register second instance of DialogService
]
//module-b.service
constructor( @Inject(dialogServiceToken) private dialogService: DialogService) {} // here we inject the second instance of a service
Andrei in DialogService
就足够了
@Component({
templateUrl: './....component.html',
providers: [DialogService],
})