如何以正常方式和角度材质对话框使用相同的组件? [英] How to use same component in normal way and with angular material dialog both?
问题描述
我有一个要从 2 个地方使用的组件
I have a component which I'm trying to use from 2 places
- 通常,在一些模板中包含选择器,例如
. - 角度材质对话框内部.
当使用带有角度材质对话框的相同组件时,我必须注入以下依赖项
When using the same component with angular material dialog, I have to inject the below dependencies
constructor(
public dialogRef: MatDialogRef<CTConfigurationComponent>,
@Inject(MAT_DIALOG_DATA) public dialogData: any,
) {}
它在案例 2 中工作正常,但案例 1 因以下错误而中断
It is working fine with case 2, but case 1 is breaking with below error
尝试使用 @Optional()
, @Skip()
但没有成功.
Tried using @Optional()
, @Skip()
but no success.
问题 -1:1. 是否可以告诉 angular DI 跳过一些依赖项?
Question -1 : 1. Is it possible to tell angular DI to skip few dependencies?
尝试了公共注入器:@Injector
,并在构造函数中调用
Tried public injector: @Injector
, and in constructor calling
this.dialogRef = this.injector.get(MatDialogRef<CTConfigurationComponent>);
也不起作用.
EDIT-1:
(method) Injector.get(token: any, notFoundValue?: any)
问题 - 2:
任何建议或解释都会有所帮助
Any suggestion or explanation will be helpful
推荐答案
在尝试了很多事情之后,我找到了解决此错误的方法.在这里发布解决方案,以便对其他人有帮助
After trying lots of things, I have got a workaround for this error. Posting the solution here so that it may be helpful to someone else
所以而不是这个
constructor(
public dialogRef: MatDialogRef<CTConfigurationComponent>,
@Inject(MAT_DIALOG_DATA) public dialogData: any
) {}
我正在使用以下代码
private dialogRef = null;
private dialogData;
constructor(private injector: Injector) {
this.dialogRef = this.injector.get(MatDialogRef, null);
this.dialogData = this.injector.get(MAT_DIALOG_DATA, null);
}
这篇关于如何以正常方式和角度材质对话框使用相同的组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!