我使用Material SnackBar来显示消息,并为SnackBar添加了一个额外的组件。在该组件中,我希望根据数据/消息动态更改panelClass值,而不是每次都在openFromComponent方法中传递panelClass参数。
有没有办法做到这一点?我在文档中找不到任何东西。在下面的代码中,您可以看到我的SnackBar组件TS。
export class SnackbarComponent implements OnInit {
constructor(public snackBarRef: MatSnackBarRef<SnackbarComponent>,
@Inject(MAT_SNACK_BAR_DATA) public data: any,
@Inject(MAT_SNACK_BAR_DEFAULT_OPTIONS) public options: any) { }
}我正在使用我的SnackBar组件:
this._snackBar.openFromComponent(FewoSysSnackbarComponent, {
data: {type: 'success/warn/info/error', text: 'MESSAGE'}
});发布于 2019-07-04 04:37:25
我找到了一个解决方案,并实现了一个使用SnackBar组件的服务,下面是我的代码:
import { Injectable } from '@angular/core';
import {MatSnackBar} from '@angular/material';
import {SnackbarComponent} from '../module/shared/snackbar/snackbar.component';
@Injectable({
providedIn: 'root'
})
export class SnackBarService {
constructor(public snackBar: MatSnackBar) {}
show(message: string, type?: string, duration?: any) {
this.snackBar.openFromComponent(SnackbarComponent, {
data: {type: type ? type : 'info', text: message},
panelClass: type ? type : 'info',
duration: duration ? duration : 0,
horizontalPosition: 'right'
});
}
}有了duration = 0,快餐店就不会消失了。
https://stackoverflow.com/questions/56876944
复制相似问题