[英]service not singleton inside MatDialog module
When I open a Mat Dialog popup ( angular material ) and accessing a singleton service, for some reason inside the mat dialog the service seems to be a new instance of the service and not the singleton service used across the app. 当我打开Mat对话框弹出窗口(有角度的材质)并访问单例服务时,由于某种原因,在mat对话框内,该服务似乎是该服务的新实例,而不是整个应用程序中使用的单例服务。
I know how to make workaround but I prefer understanding if there is a known issue with matdialog and maybe a fix. 我知道如何解决,但我更喜欢了解matdialog是否存在已知问题,以及是否可以解决。
const dialogRef = this.dialog.open(MyComponent, {
width: '400px',
data: {
myInfo: Info
},
});
The service was added in the "App.component.ts" as a singleton like this: 该服务作为一个单例添加到“ App.component.ts”中,如下所示:
import {Component, OnInit} from '@angular/core';
import {NavigationEnd, Router} from '@angular/router';
import {AuthService} from './shared/services/auth.service';
@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.scss'],
providers: [AuthService]}) export class AppComponent implements OnInit {}
Thanks! 谢谢!
As far as I know, the problem is that you are declaring service
inside AppComponent
, but material dialogs
are spawning outside your app-root
( angular cdk
is spawning as last element in body
) so it's not available for your dialog component
. 据我所知,问题在于您在AppComponent
内声明service
,但是material dialogs
在您的app-root
( angular cdk
作为body
最后一个元素生成),因此它不适用于您的dialog component
。 To fix this you should declare provider AuthSerivce
inside your main AppModule
then it will be visible in your dialog component. 要解决此问题,您应该在主AppModule
声明提供程序AuthSerivce
然后在对话框组件中将其可见。
I am running into the same issue. 我遇到了同样的问题。 Have you had any answers to this question? 您对此问题有任何答案吗? I'm also using mat-dialog, when i call a service (with providedIn root), the dialog creates a new instance of the service. 我还使用mat-dialog,当我调用服务(具有ProvideIn根目录)时,对话框会创建该服务的新实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.