LoginSignup
18
19

More than 3 years have passed since last update.

Angular Materialを使ってダッシュボードを実装する

Last updated at Posted at 2018-12-18

Angular MaterialのSchematicsを使って、3分でダッシュボードを実装する。

コマンドをいくつか叩く。

$ ng new schematics-sample
? Would you like to add Angular routing? Yes
$ ng add @angular/material
$ ng g @angular/material:address-form form
$ ng g @angular/material:nav nav
$ ng g @angular/material:table table
$ ng g @angular/material:dashboard dashboard
$ ng g @angular/material:tree tree

ファイルを3つだけ修正する。

app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { DashboardComponent } from './dashboard/dashboard.component';
import { FormComponent } from './form/form.component';
import { TableComponent } from './table/table.component';
import { TreeComponent } from './tree/tree.component';

const routes: Routes = [
  { path: '', component: DashboardComponent },
  { path: 'form', component: FormComponent },
  { path: 'table', component: TableComponent },
  { path: 'tree', component: TreeComponent }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }
nav.component.html
<mat-sidenav-container class="sidenav-container">
  <mat-sidenav #drawer class="sidenav" fixedInViewport="true"
      [attr.role]="(isHandset$ | async) ? 'dialog' : 'navigation'"
      [mode]="(isHandset$ | async) ? 'over' : 'side'"
      [opened]="!(isHandset$ | async)">
    <mat-toolbar>Menu</mat-toolbar>
    <mat-nav-list>
      <a mat-list-item [routerLink]="['/']">Top</a>
      <a mat-list-item [routerLink]="['/form']">Form</a>
      <a mat-list-item [routerLink]="['/table']">Table</a>
      <a mat-list-item [routerLink]="['/tree']">Tree</a>
    </mat-nav-list>
  </mat-sidenav>
  <mat-sidenav-content>
    <mat-toolbar color="primary">
      <button
        type="button"
        aria-label="Toggle sidenav"
        mat-icon-button
        (click)="drawer.toggle()"
        *ngIf="isHandset$ | async">
        <mat-icon aria-label="Side nav toggle icon">menu</mat-icon>
      </button>
      <span>schematics-sample</span>
    </mat-toolbar>
    <ng-content></ng-content>
  </mat-sidenav-content>
</mat-sidenav-container>
app.component.html
<app-nav>
  <router-outlet></router-outlet>
</app-nav>

ダッシュボードの実装完了です。

スクリーンショット 2018-12-18 12.06.13.png
スクリーンショット 2018-12-18 12.06.19.png
スクリーンショット 2018-12-18 12.06.23.png
スクリーンショット 2018-12-18 12.06.26.png

18
19
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
19