【问题标题】:Snackbar not working on Modal Table Angular MaterialSnackbar 不适用于模态表角材料
【发布时间】:2020-11-13 08:20:58
【问题描述】:

我已经实现了删除功能,成功完成后,我必须显示一个带有从后端收到的消息的小吃栏。我能够得到正确的响应,但无法访问小吃店。我认为范围不同,因为它是我要删除用户的 mat-dialog 表。 TS 代码:

@Component({
  selector: 'app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})

export class AppComponent implements OnInit {
constructor(
      public commonService :CommonService
    } {}
// I am opening dialog from this parent component
 openDialog(u, c){
     this.response = JSON.parse(JSON.parse(u.Data));
     for(var i =0;i<Object.keys(this.response).length;i++){
      this.final.push(this.response[i]);
    }
     const dialogRef = this.dialog.open(DialogExample, {
         data: {arrayDialog: this.finalUsers, c}
       });
     dialogRef.afterClosed().subscribe(result => {
       this.finalUsers = [];
     });
   }


//this is where the mat dialog is
 @Component({
    selector: 'dialog-example',
    templateUrl: 'dialog.html',
  })
  export class DialogExample {
    constructor(
      public dialogRef: MatDialogRef<DialogExample>,
      public dialog:MatDialog,
      public commonService :CommonService,
    } {}
//some function
              //if deletion is successful
              dialogRef.afterClosed().subscribe((confirmed: boolean) => {
              if (confirmed) { //if deletion is confirmed
              this.deleteMessage = this.details.msg;
              this.commonService.showSnackBar(this.deleteMessage);           

通用服务代码:

import { Injectable } from '@angular/core';
import { Subject, Observable } from 'rxjs';
import { MatSnackBar } from '@angular/material';

@Injectable({
  providedIn: 'root'
})
export class CommonService {

  public spinnerSubject: Subject<boolean> = new Subject<boolean>();

  constructor(private snackBar: MatSnackBar) {}

  public showSnackBar(message){
    this.snackBar.open(message,'',{
      duration: 5000   
    });}}

如何显示带有消息的快餐栏?我收到以下错误:错误类型错误:无法读取未定义的属性“showSnackBar”。我注意到我无法从 afterClosed 内部调用任何函数和服务。有什么解决方法吗?

【问题讨论】:

  • 更新公共服务代码
  • 更新公共服务代码如?这是我创建的一个普通服务,我在多个组件中使用它。我也在父组件中使用它,它工作正常。我无法在垫子对话框中访问它
  • 是的。在这里更新代码
  • 我已经更新了
  • 您能否提供一个指向最小可重复样本的链接?

标签: angular typescript angular-material angular8


【解决方案1】:

通用服务代码:

试试这个..

import { Injectable } from '@angular/core';
    import { Subject, Observable } from 'rxjs';
    import { MatSnackBar, MatSnackBarConfig } from '@angular/material';
    
    @Injectable({
      providedIn: 'root'
    })
    export class CommonService {
    
      public spinnerSubject: Subject<boolean> = new Subject<boolean>();
    
      constructor(private snackBar: MatSnackBar) {}
    
      public showSnackBar(message){
        let config = new MatSnackBarConfig();
        config.duration = 5000;
        this.snackBar.open(message,undefined,config);
      }
    }

【讨论】:

  • this.snackBar.open("信息删除成功!", undefined, config);
  • 我在编辑中添加了一条重要信息。你现在可以看看吗。我想我以某种方式错过了它。实际上 delete 是在 afterClosed() 上调用的,所以我不能在那里调用任何服务或函数。有什么办法可以解决这个问题?
  • 我添加了打开对话框的部分
猜你喜欢
  • 2017-07-08
  • 1970-01-01
  • 2019-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-01
  • 1970-01-01
相关资源
最近更新 更多