Angular Material 7 - 按鈕



<mat-button> 是一個 Angular 指令,用於建立具有 Material 風格和動畫的按鈕。

在本章中,我們將展示使用 Angular Material 繪製按鈕控制元件所需的配置。

建立 Angular 應用

請按照以下步驟更新我們在 Angular 6 - 專案設定 章節中建立的 Angular 應用:

步驟 描述
1 按照 Angular 6 - 專案設定 章節中說明的方法,建立一個名為 materialApp 的專案。
2 修改 app.module.tsapp.component.tsapp.component.cssapp.component.html,如下所述。保持其餘檔案不變。
3 編譯並執行應用程式以驗證已實現邏輯的結果。

以下是修改後的模組描述符 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,MatIconModule} from '@angular/material'
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
@NgModule({
   declarations: [
      AppComponent
   ],
   imports: [
      BrowserModule,
      BrowserAnimationsModule,
      MatButtonModule,MatIconModule,
      FormsModule,
      ReactiveFormsModule
   ],
   providers: [],
   bootstrap: [AppComponent]
})
export class AppModule { }

以下是修改後的 CSS 檔案 app.component.css 的內容。

.tp-button-row button,
.tp-button-row a {
   margin-right: 8px;
}

以下是修改後的 HTML 主檔案 app.component.html 的內容。

<div class = "example-button-row">
   <button mat-button>Basic</button>
   <button mat-raised-button>Raised</button>
   <button mat-stroked-button>Stroked</button>
   <button mat-flat-button>Flat</button>
   <button mat-icon-button>
      <mat-icon aria-label="Heart">favorite</mat-icon>
   </button>
   <button mat-fab>Fab</button>
   <button mat-mini-fab>Mini</button>
   <a mat-button routerLink = ".">Link</a>
</div>

結果

驗證結果。

Buttons

詳情

  • 在這裡,我們使用各種 mat-buttons 變體建立了按鈕。
廣告