Angular-material7-snackbar

提供:Dev Guides
移動先:案内検索

角材7-スナックバー

Angular Directiveの <MatSnackBar> は、ダイアログ/ポップアップの代替としてモバイルデバイスに表示する通知バーを表示するために使用されます。

この章では、Angular Materialを使用してスナックバーを表示するために必要な構成を紹介します。

変更されたモジュール記述子 app.module.ts の内容は次のとおりです。

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {MatButtonModule,MatSnackBarModule} from '@angular/material'
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
@NgModule({
   declarations: [
      AppComponent
   ],
   imports: [
      BrowserModule,
      BrowserAnimationsModule,
      MatButtonModule,MatSnackBarModule,
      FormsModule,
      ReactiveFormsModule
   ],
   providers: [],
   bootstrap: [AppComponent]
})
export class AppModule { }

以下は、変更されたHTMLホストファイル app.componentl の内容です。

<button mat-button (click)="openSnackBar('Party', 'act')">Show snack-bar</button>

変更されたtsファイル app.component.ts の内容は次のとおりです。

import {Component, Injectable} from '@angular/core';
import { MatSnackBar } from "@angular/material";
@Component({
   selector: 'app-root',
   templateUrl: 'app.componentl',
   styleUrls: ['app.component.css']
})
export class AppComponent {
   constructor(public snackBar: MatSnackBar) {}
   openSnackBar(message: string, action: string) {
      this.snackBar.open(message, action, {
         duration: 2000,
      });
   }
}

結果

結果を確認します。

SnackBar

詳細

  • ここでは、マットボタンを使用してボタンを作成し、そのボタンをクリックするとスナックバーが表示されます。