Angular Material中的MatSnackBar

Angular Material中的MatSnackBar

Angular Material是一个由Angular团队开发的UI组件库,用于构建桌面和移动Web应用程序的设计组件。为了安装它,我们需要在我们的项目中安装Angular,一旦你完成了它,你可以输入下面的命令,就可以下载它。MatSnackBar用于在执行任何操作时从屏幕底部显示信息或文本。

安装:

ng add @angular/material

步骤:

  • 首先,使用上述命令安装Angular Material。
  • 完成安装后,在app.module.ts文件中从’@angular/material/snack-bar’导入’MatSnackBarModule’。
  • 首先,我们需要为’MatSnackBar’创建一个实例。然后我们需要在点击按钮时调用一个函数。
  • 使用这个实例,我们可以访问内置的open()函数。
  • 现在我们需要提到信息的持续时间。
  • 一旦完成了上述步骤,就可以服务或开始项目。

代码实现:

import { NgModule } from '@angular/core';  
import { BrowserModule } from '@angular/platform-browser';  
import { FormsModule } from '@angular/forms';  
        
import { AppComponent } from './app.component';  
import { BrowserAnimationsModule } from  
    '@angular/platform-browser/animations'; 
import {MatSnackBarModule} from '@angular/material/snack-bar';
import { MatButtonModule } from '@angular/material/button';  
    
@NgModule({  
    imports: [  
        BrowserModule,  
        FormsModule,  
        MatButtonModule, 
        MatSnackBarModule, 
        BrowserAnimationsModule 
    ],  
    declarations: [ AppComponent ],  
    bootstrap: [ AppComponent ]  
})  
export class AppModule { }
import { Component } from '@angular/core';
import {MatSnackBar} from '@angular/material/snack-bar';
  
  
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  
  constructor(private _snackBar: MatSnackBar) {}
  
  openSnackBar(message: string, action: string) {
    this._snackBar.open(message, action, {
      duration: 2000,
    });
  }
}

app.component.html:

<button mat-stroked-button (click)=
    "openSnackBar('GAME ONE','HURRAH !!!!!')">
    Show snack-bar Message
</button>
  
<p>
    By Clicking on above button we can invoke 
    the function and it will, render the 
    message of snack-bar
</p>

输出:

Angular Material中的MatSnackBar

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程