【发布时间】: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