1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Angular MaterialでカスタムのIconを使う

Posted at

公式ドキュメントに書いてありますが、忘れそうだったので。

app.component.ts
import { Component } from '@angular/core';
import { MatIconRegistry } from '@angular/material/icon';
import { DomSanitizer } from '@angular/platform-browser';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  constructor(
    private domSanitizer: DomSanitizer,
    private iconRegistry: MatIconRegistry,
  ) {
    this.iconRegistry.addSvgIcon(
      'cut', // ここで名前を指定
      this.domSanitizer.bypassSecurityTrustResourceUrl('/assets/icons/cut.svg') //必ずサニタイザーを利用して取得する。
    );
  }
}
なんかのhtml
<mat-icon svgIcon="cut"></mat-icon>

これだけだと、ちょっとsvgが大きくて、他のAngular Material Iconとサイズ感が違うので、僕はこんな感じでcssを当ててます。これは使うSVGによると思いますので、あくまでメモ程度まで。

どこかのscss
mat-icon[svgIcon] {
    svg {
        width: 1.2em;
        margin: auto;
    }
}
1
3
0

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
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?