最佳实践是创建一个新组件,但您不需要 100 个新组件,只需 1 个即可。
你可以创建一个snackbar消息组件,你可以注入任何你想描述行为的对象@Inject(MAT_SNACK_BAR_DATA)
;
如果你有一个对象snackbar-message-data.ts
export interface SnackbarMessageData {
checkable: boolean;
text: string;
action: string;
icon: string;
}
@Component({
template: `
<input *ngIf="data.checkable" type="checkbox" />
<mat-icon *ngIf="data.icon">{{ data.icon }}</mat-icon>
<span>{{ data.text }}</span>
<button
mat-raised-button
color="accent"
(click)="snackBarRef.dismiss()"
>
{{ data.action }}
</button>
`,
})
export class SnackbarMessageComponent {
constructor(
public snackBarRef: MatSnackBarRef<TestNotificationComponent>
@Inject(MAT_SNACK_BAR_DATA) public data: any
) { }
}
从打开快餐栏的父组件:
this.snackbar.openFromComponent(SnackbarMessageComponent, {
checkable: true,
text: `${this.products?.length} Successfully Added`,
action: 'Close'
});