Angular 6: ERROR TypeError: Cannot read property 'afterClosed' of undefined Angular 6: 错误类型:无法读取未定义属性 'afterClosed'
创始人
2024-10-16 05:00:30
0

这个错误通常发生在使用Angular Material对话框时,afterClosed属性未正确定义。

下面是一个可能导致此错误的示例代码:

import { Component, OnInit } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';

@Component({
  selector: 'app-example',
  template: `
    
  `,
})
export class ExampleComponent implements OnInit {
  constructor(private dialog: MatDialog) {}

  ngOnInit() {}

  openDialog() {
    const dialogRef = this.dialog.open(DialogComponent);

    dialogRef.afterClosed().subscribe(result => {
      console.log(`Dialog result: ${result}`);
    });
  }
}

@Component({
  selector: 'app-dialog',
  template: `
    

Dialog Title

Dialog Content `, }) export class DialogComponent {}

要解决此错误,确保在打开对话框之前,先检查dialogRef是否被正确赋值。

您可以通过将对话框引用存储在类属性中来实现:

import { Component, OnInit } from '@angular/core';
import { MatDialog, MatDialogRef } from '@angular/material/dialog';

@Component({
  selector: 'app-example',
  template: `
    
  `,
})
export class ExampleComponent implements OnInit {
  dialogRef: MatDialogRef;

  constructor(private dialog: MatDialog) {}

  ngOnInit() {}

  openDialog() {
    this.dialogRef = this.dialog.open(DialogComponent);

    this.dialogRef.afterClosed().subscribe(result => {
      console.log(`Dialog result: ${result}`);
    });
  }
}

@Component({
  selector: 'app-dialog',
  template: `
    

Dialog Title

Dialog Content `, }) export class DialogComponent {}

通过这种方式,确保dialogRef有效,并且能够正确访问afterClosed属性,以避免出现"Cannot read property 'afterClosed' of undefined"错误。

相关内容

热门资讯

总算了解!aapoker插件,... 1、总算了解!aapoker插件,德扑圈透视挂,安装教程(有挂透视)2、进入游戏-大厅左侧-新手福利...
实测教程!hhpoker软件安... 实测教程!hhpoker软件安装包,wepoker软件安装包,德州论坛(有挂技巧);实战中需综合运用...
一分钟了解!pokemmo手机... 一分钟了解!pokemmo手机脚本辅助器,pokerworld破解版下载,教你攻略(有挂软件);实战...
解密关于!wepoker怎么获... 解密关于!wepoker怎么获得好牌,we-poker是什么软件,必胜教程(有挂教程);1分钟了解详...
必备攻略!wepoker私人局... 您好,德普之星辅助工具如何设置这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很...
玩家必看教程!aapoker辅... 玩家必看教程!aapoker辅助器是真的吗,wpk透视辅助方法,玩家教你(有挂方法)关于aapoke...
科技通报!pokeplus脚本... 科技通报!pokeplus脚本,德普软件,线上教程(有挂教程);大神普及一款德州ai内幕,确定了po...
揭秘!hhpoker哪个俱乐部... 您好,hhpoker哪个俱乐部靠谱这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302...
推荐一款!wepoker有插件... 1、推荐一款!wepoker有插件吗,wepoker智能辅助插件,扑克教程(有挂方法)。2、wepo...
一分钟带你了解!线上德州的辅助... 一分钟带你了解!线上德州的辅助器是什么,红龙poker有辅助吗,软件教程(有挂软件);原来确实真的有...