我想在我的Angular 4项目中使用从https://materialdesignicons.com/开始的图标. 网站上的说明仅涵盖如何将其纳入Angular 1.x(https://materialdesignicons.com/getting-started)

我怎样才能包含material 设计图标,以便像使用Angular Materialng serve一样使用它们?

NOTE: I already Included the 100 which are different!

推荐答案

关于如何将material 设计图标包含到Angular Material应用程序中的说明,现在可以在Material Design Icons - Angular文档页面上找到.

TL;DR:您现在可以利用@mdi/angular-material NPM软件包,其中包括作为单个SVG文件分发的MDI图标(mdi.svg):

npm install @mdi/angular-material

然后,通过将此SVG文件包含在angular.json中项目的assets配置属性中,即可将其包含到您的应用程序中:

{
  // ...
  "architect": {
    "build": {
      "options": {
        "assets": [
          { "glob": "**/*", "input": "./assets/", "output": "./assets/" },
          { "glob": "favicon.ico", "input": "./", "output": "./" },
          { "glob": "mdi.svg", "input": "./node_modules/@mdi/angular-material", "output": "./assets" }
        ]
      }
    }
  }
  // ...
}

应用程序的主模块还需要声明必要的导入(@angular/common/http中的HttpClientModule个用于加载图标,@angular/material/icon中的MatIconModule个用于加载图标),以及将图标集添加到注册表:

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

@NgModule({
  imports: [
    // ...
    HttpClientModule,
    MatIconModule
  ]
})
export class AppModule {
  constructor(iconRegistry: MatIconRegistry, domSanitizer: DomSanitizer) {
    iconRegistry.addSvgIconSet(
      domSanitizer.bypassSecurityResourceHtml('./assets/mdi.svg')
    );
  }
}

现在还可以买到StackBlitz demo.

旧版本Angular的步骤如下所述:


只需执行以下步骤:

  1. Angular Material部分下的here下载mdi.svg,并将其放在assets文件夹中,该文件夹应位于(从项目的根目录)/src/assets:

    Documentation assets folder

  2. 在应用程序的模块(又名app.module.ts)中,添加以下行:

    import {MatIconRegistry} from '@angular/material/icon';
    import {DomSanitizer} from '@angular/platform-browser';
    ...
    export class AppModule {
      constructor(private matIconRegistry: MatIconRegistry, private domSanitizer: DomSanitizer){
        matIconRegistry.addSvgIconSet(domSanitizer.bypassSecurityResourceUrl('/assets/mdi.svg'));
      }
    }
    
  3. 确保在assets中包括.angular-cli.json以下的assets文件夹(尽管默认情况下,它将在那里):

    {
      "apps": [
        {
          ...
          "assets": [
            "assets"
          ]
        }
      ]
    }
    
  4. 最后,通过带有svgIcon输入的MatIcon组件使用它:

    <mat-icon svgIcon="open-in-new"></mat-icon>
    

Angular相关问答推荐

RFDC.CommonJS或AMD依赖项可能会导致优化救助ANGURAL PROCEMENT with ngx-charts lib

接收错误NullInjectorError:R3InjectorError(_AppModule)[config—config]....>过度安装Angular—Markdown—Editor

将Angular 17中的http服务与独立组件一起使用

角material 17 -如何从Style.scss中的主题中获取原色

图像未显示在视图屏幕上-Angular 投影

截获火灾前调用公共接口

无法在Mat-SideNav中绑定模式

在Angular 17的本地服务应用程序时禁用SSR

如何将 Firebase 云消息传递 (FCM) 与 Angular 15 结合使用?

npm install命令在基本Angular 应用程序的天蓝色构建管道中失败

Cypress 12.8.1 无法使用条纹元素 iframe

Angular 14 类型表单 - 数字控件的初始值

Angular 4 - 在下拉列表中 Select 默认值 [Reactive Forms]

如何处理解析器中的错误

如何以Angular 获取嵌套表单组的控件

Angular2 Base64 清理不安全的 URL 值

如何在 Angular 2 中创建可重用的动画

如何设置背景 colored颜色 IONIC 4

如何在功能模块层次 struct 中使用 .forRoot()

如何在 angular2 中的 div 的 contenteditable 上使用 [(ngModel)]?