当前位置: 首页 > news >正文

如何在 Angular Material 中使用自定义 SVG 图标

简介

Angular Material 库提供了一套使用 Material Design 样式的 Angular 组件。其中之一是 <mat-icon> 组件。有许多现成的 Material 图标。但如果我们想在保持 Material Design 样式一致的同时显示一些自定义图标怎么办?让我们学习如何在 Angular Material 组件中使用自定义 SVG 图标。

在本教程中,您将使用 <mat-icon> 组件来使用标准的 Material Icons 字体。然后,您将使用 <mat-icon> 组件来支持自定义 SVG 图标。

完整的可工作代码可以在这个 GitHub 仓库中找到。

先决条件

要完成本教程,您需要:

  • 本地安装了 Node.js,您可以按照《如何安装 Node.js 并创建本地开发环境》进行操作。

本文假设您对 Angular v4.2+ 有一些基本知识。

本教程最初是使用 Angular v5.2+ 和 Angular Material v5.2.4 编写的。

本教程已经验证过可以在 Angular v10.0.5 和 Angular Material v10.1.1 上运行。

如果您刚开始使用 Angular Material,可以参考本文。

步骤 1 —— 创建 Angular 项目并安装依赖项

首先,打开您的终端并创建一个新的项目目录:

mkdir angular-material-custom-svg

接下来,进入新目录:

cd angular-material-custom-svg

然后,使用 npm 安装 Angular CLI 作为 devDependency

npm install @angular/cli@10.0.4 --save-dev

现在,您可以使用 Angular CLI 创建一个新的 Angular 项目,并为本教程的需要设置一些选项:

ng new angular-material-custom-svg --directory=. --skipTests=true --routing=false --style=css

这样就在当前目录中得到了一个全新的 Angular 项目。让我们使用以下命令安装 Angular Material 及其依赖项:

npm install @angular/material@10.1.1 @angular/cdk@10.1.1 --save

这样就完成了教程项目的设置。现在我们可以继续在项目中使用 Material 图标。

步骤 2 —— 使用 <mat-icon> 显示图标字体

要使用默认的 Material 图标,您首先需要在全局样式表中导入它们。为此,请打开由 Angular CLI 生成的 styles.css 文件:

nano src/styles.css

用以下 import 语句替换文件的内容:

@import url("https://fonts.googleapis.com/icon?family=Material+Icons");

接下来,您需要导入 MatIconModule。打开 app.module.ts 文件:

nano src/app.module.ts

然后,添加 MatIconModuleimport

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { MatIconModule } from "@angular/material/icon";

并将其添加到模块的 imports 数组中:

@NgModule({declarations: [AppComponent],imports: [BrowserModule,MatIconModule],providers: [],bootstrap: [AppComponent]
})

现在,您可以使用 <mat-icon> 组件显示内置的 Material 图标。如果您添加图标的文本名称,它将显示关联的图标字形。

对于我们的教程,我们将添加 "mood" 图标(这类似于带笑脸的符号):

!Material 图标字形的 mood

Material Design 网站上有一个完整的 Material 图标列表,您可以直接使用。

打开 app.component.html 文件:

nano src/app/app.component.html

用以下代码替换文件的内容:

<mat-icon>mood</mat-icon>

让我们来看看我们到目前为止做了什么!启动应用程序:

ng serve

在您的 Web 浏览器中查看应用程序(localhost:4200),您将看到 "mood" 图标。

这样就完成了使用 <mat-icon> 显示图标字体。我们现在可以继续使用 <mat-icon> 显示 SVG。

步骤 3 —— 使用 <mat-icon> 显示 SVG

让我们向项目中添加一个自定义 "unicorn" 图标。

将图标保存为 unicorn_icon.svg,并放到项目的 src/assets 文件夹中。

要使用我们的自定义 unicorn 图标与 <mat-icon> 组件标签一起,我们需要导入 HttpClientModule

打开 app.module.ts 文件:

nano src/app/app.module.ts

然后,添加 HttpClientModuleimport

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { MatIconModule } from "@angular/material/icon";
import { HttpClientModule } from "@angular/common/http";

并将其添加到模块的 imports 数组中:

@NgModule({declarations: [AppComponent],imports: [BrowserModule,MatIconModule,HttpClientModule],providers: [],bootstrap: [AppComponent]
})

现在,我们可以使用 Angular Material 提供的 MatIconRegistry 服务注册我们的自定义 "unicorn" 图标。

打开 app.component.ts

nano src/app/app.component.ts

然后,添加 MatIconRegistryimport

import { Component } from "@angular/core";
import { MatIconRegistry } from "@angular/material/icon";

并将该服务注入到组件中:

export class AppComponent{constructor(private matIconRegistry: MatIconRegistry){// ...}
}

在组件的 constructor 方法中添加 addSvgIcon 方法:

export class AppComponent{constructor(private matIconRegistry: MatIconRegistry){this.matIconRegistry.addSvgIcon(`icon_label`,`path_to_custom_icon.svg`);}
}

addSvgIcon 方法通过两个参数注册我们的图标。

第一个参数是图标标签,类型为 string

第二个参数是指向图标文件位置的相对 URL 路径。

这个 URL 路径字符串的类型是 SafeResourceUrl。为了将 URL 路径字符串解析为 SafeResourceUrl,我们可以使用 Angular 提供的 DomSanitizer

接下来,添加 DomSanitizerimport

import { Component } from "@angular/core";
import { MatIconRegistry } from "@angular/material/icon";
import { DomSanitizer } from "@angular/platform-browser";

并将该服务注入到组件中:

export class AppComponent{constructor(private matIconRegistry: MatIconRegistry,private domSanitizer: DomSanitizer){this.matIconRegistry.addSvgIcon(`icon_label`,`path_to_custom_icon.svg`);}
}

给定一个相对 URL 路径字符串,DomSanitizer 上的 bypassSecurityTrustResourceUrl 方法将返回一个安全的资源 URL,这是 addSvgIcon 方法所需的。

现在,您可以将icon_label替换为自定义的 "unicorn" 标签。将 path_to_custom_icon.svg 替换为自定义的 "unicorn_icon.svg" 图标。

让我们更新 addSvgIcon 中的行:

export class AppComponent{constructor(private matIconRegistry: MatIconRegistry,private domSanitizer: DomSanitizer){this.matIconRegistry.addSvgIcon("unicorn",this.domSanitizer.bypassSecurityTrustResourceUrl("../assets/unicorn_icon.svg"));}
}

现在,自定义的 "unicorn" 图标已经正确注册到 MatIconRegistry 服务中。

要显示自定义图标,您需要更新组件的模板。打开 app.component.html

nano src/app/app.component.html

并将图标标签传递给 <mat-icon>svgIcon 输入属性:

<mat-icon svgIcon="unicorn"></mat-icon>

让我们来看看您到目前为止做了什么!启动应用程序:

ng serve

在您的 Web 浏览器中查看应用程序(localhost:4200),您将看到自定义图标以 Material 样式显示。

现在,您可以在应用程序中使用 Material 样式显示一整套自定义图标。

为了使代码更清晰、更易维护,我们可以通过将 MatIconRegistry 移入一个服务类来重构代码。

结论

在本教程中,您已经完成了对使用 Angular Material 的 <mat-icon> 组件与 Material Icons 字体和自定义 SVG 的初步探索。这为您提供了一种在应用程序中保持一致地遵循 Material Design 样式的方法。

相关文章:

  • 【洛谷 P3367】【模板】并查集 题解(并查集+启发式合并)
  • c++类和对象新手保姆级上手教学(上)
  • The method toList() is undefined for the type Stream
  • 汇编的两道题
  • ES入门知识点总结
  • ChatGPT高效提问—prompt实践(智能辅导-心理咨询-职业规划)
  • 互联网加竞赛 基于计算机视觉的身份证识别系统
  • 前端工程化面试题 | 11.精选前端工程化高频面试题
  • Ubuntu忘记登录密码重置步骤
  • 使用 Spring Data JPA 和 Mybatis 结合的方式进行分页查询
  • 1414 - 期末考试成绩排名
  • 【分享】JLINK的SW调试模式连线方式
  • 【深度学习】S2 数学基础 P4 概率论
  • uniapp如何给视频组件设置图片
  • leetcode135. 分发糖果
  • 4个实用的微服务测试策略
  • Angular4 模板式表单用法以及验证
  • Apache Pulsar 2.1 重磅发布
  • bootstrap创建登录注册页面
  • JavaScript新鲜事·第5期
  • php面试题 汇集2
  • quasar-framework cnodejs社区
  • Terraform入门 - 3. 变更基础设施
  • 关于springcloud Gateway中的限流
  • 实现简单的正则表达式引擎
  • 详解移动APP与web APP的区别
  • 用简单代码看卷积组块发展
  • 《天龙八部3D》Unity技术方案揭秘
  • ionic异常记录
  • PostgreSQL之连接数修改
  • Prometheus VS InfluxDB
  • #大学#套接字
  • (2015)JS ES6 必知的十个 特性
  • (Java实习生)每日10道面试题打卡——JavaWeb篇
  • (原创)Stanford Machine Learning (by Andrew NG) --- (week 9) Anomaly DetectionRecommender Systems...
  • (转)清华学霸演讲稿:永远不要说你已经尽力了
  • (转载)利用webkit抓取动态网页和链接
  • .form文件_一篇文章学会文件上传
  • .NetCore部署微服务(二)
  • [2016.7 day.5] T2
  • [AutoSar]BSW_OS 02 Autosar OS_STACK
  • [CERC2017]Cumulative Code
  • [CSS] - 修正IE6不支持position:fixed的bug
  • [ffmpeg] x264 配置参数解析
  • [iOS]中字体样式设置 API
  • [Java] 图说 注解
  • [Java][方法引用]构造方法的引用事例分析
  • [LeetCode周赛复盘] 第 312 场周赛20220925
  • [Linux] - 定时任务crontab
  • [Spring]一文明白IOC容器和思想
  • [vijos1554bzoj1411]硬币游戏快速幂
  • [Windows] Win11 常用快捷键
  • [xPlugin] smartupload jsp图片上传
  • [爱情] 『转载』女生写的追MM秘籍,看了马上告别光棍
  • [编程题]数据库连接池 - 牛客网题解