【问题标题】:How to access members in typeof instance of Component?如何访问组件类型实例中的成员?
【发布时间】:2019-09-24 17:43:08
【问题描述】:

我在我的应用程序中使用MatDialog 的角度材料来弹出窗口。在这里,我创建了一个父弹出组件ParentComponent,其中我将ChildComponent 作为参数传递并在父组件的构造函数中访问该子组件。但是,这个参数的类型是typeof。我想访问成员,例如:子组件内部的TestFunction 使用此typeof 实例进入父组件。

基本上,我有弹出窗口的关闭方法,它应该调用保存按钮子组件的TestFunction() 代码如下:

  1. 将子组件作为参数传递给父组件:

    constructor(private _dialog: MatDialog){}
    let dialogRef = this._dialog.open(ParentComponent, {
      data: { component: ChildComponent }
    });
    
  2. 子组件:

    TestFunction(){ console.log('test message')}
    
  3. 在父组件中访问:

     constructor(public dialogRef: MatDialogRef<ParentComponent>,
              @Inject(MAT_DIALOG_DATA) public data: any) { }
    
      ngOnInit() {
         console.log('data in component: ',this.data.prototype);
    
         this.data.TestFunction();
    
     }
    

在子组件的控制台中,我得到:

data in component:  
{component: ƒ}
component: class ChildComponent
ctorParameters: () => {…}
arguments: (...)
caller: (...)
length: 3

但我想要 TestFunction()。

【问题讨论】:

  • 打开对话框时返回的matDialogRef 有一个属性componentInstance,您可以通过它访问组件的功能

标签: javascript angular angular-material


【解决方案1】:
dialogRef: MatDialogRef<YourComponent>;

this.dialogRef = this.matDialog.open(YourComponent, {
     disableClose: false
});

// Child Functionality
this.dialogRef.componentInstance.TestFunction()

【讨论】:

  • 我将componentInstance 视为未定义
  • 能否请您上传您的相关代码以及昨天的更改?在我自己的代码中遵循相同的模式会产生一个已定义的组件
  • 请注意!很高兴听到
猜你喜欢
  • 2019-07-13
  • 2014-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-19
  • 1970-01-01
  • 1970-01-01
  • 2013-10-28
相关资源
最近更新 更多