Angular Material Snackbar: 不覆盖先前的消息,而是显示新的消息。
创始人
2024-10-19 19:00:44
0

Angular Material Snackbar 默认情况下会覆盖先前的消息并显示新的消息。如果你想要不覆盖先前的消息而是显示新的消息,可以使用 MatSnackBaropenFromComponent 方法和一个自定义的组件来实现。

首先,创建一个自定义的组件,例如 CustomSnackbarComponent

import { Component, OnInit, Inject } from '@angular/core';
import { MAT_SNACK_BAR_DATA } from '@angular/material/snack-bar';

@Component({
  selector: 'app-custom-snackbar',
  template: `
    
{{ data.message }}
`, }) export class CustomSnackbarComponent implements OnInit { constructor(@Inject(MAT_SNACK_BAR_DATA) public data: any) {} ngOnInit() {} }

然后,在你的组件中使用 MatSnackBaropenFromComponent 方法来显示 Snackbar:

import { Component } from '@angular/core';
import { MatSnackBar } from '@angular/material/snack-bar';
import { CustomSnackbarComponent } from './custom-snackbar.component';

@Component({
  selector: 'app-example-component',
  template: `
    
  `,
})
export class ExampleComponent {
  constructor(private snackBar: MatSnackBar) {}

  showSnackbar() {
    this.snackBar.openFromComponent(CustomSnackbarComponent, {
      data: { message: 'New message' },
      duration: 3000, // 持续时间,单位毫秒
    });
  }
}

在上面的示例中,我们使用 MatSnackBaropenFromComponent 方法来打开 Snackbar,并将 CustomSnackbarComponent 作为组件传递给它。我们还通过 data 属性将消息传递给自定义组件。这样,每次调用 showSnackbar 方法时,都会显示一个新的 Snackbar 消息,而不会覆盖先前的消息。

相关内容

热门资讯

透视法子!wepoker软件辅... 透视法子!wepoker软件辅助程序,wepoker私人局透视教程(透视)真是真的是有脚本插件(哔哩...
透视积累!德扑之心免费透视,德... 透视积累!德扑之心免费透视,德普之星透视辅助软件是真的吗(透视)其实是有脚本挂(哔哩哔哩)1、德普之...
透视积累!aapoker插件,... 透视积累!aapoker插件,aapoker透视怎么用(透视)切实是真的透视软件(哔哩哔哩)1、aa...
透视指南!hhpoker作必弊... 透视指南!hhpoker作必弊码,sohoo poker辅助器(透视)都是存在有挂(哔哩哔哩)该软件...
透视手段!wepoker轻量版... 透视手段!wepoker轻量版有透视吗,wepoker智能辅助插件(透视)果然真的是有透视app(哔...
透视模块!wepoker透视脚... 透视模块!wepoker透视脚本下载,WePOker有没有透视方法(透视)真是是有透视器(哔哩哔哩)...
透视绝活!德州局怎么透视,wp... 透视绝活!德州局怎么透视,wpk有那种辅助吗(透视)真是是真的挂(哔哩哔哩)1、打开软件启动之后找到...
透视操作!hhpoker辅助挂... 透视操作!hhpoker辅助挂是真的吗,hhpoker德州真的假的(透视)切实有挂(哔哩哔哩)亲,关...
透视绝活!德州局怎么透视,wp... 透视绝活!德州局怎么透视,wpk有那种辅助吗(透视)真是真的有挂(哔哩哔哩)1、首先打开wpk有那种...
透视指南!wepoker好友局... 透视指南!wepoker好友局透视,购买wepoker模拟器(透视)一直真的有挂(哔哩哔哩)1、在w...