方法一:材质图标注册
Material 允许使用自定义 SVG 图标及其组件(如mat-button
)。FontAwesome 图标也是 SVG,因此您可以使用此机制来解决手头的任务。
// 1. Import desired FontAwesome icon
import { faFontAwesomeFlag } from '@fortawesome/free-brands-svg-icons';
import { icon } from '@fortawesome/fontawesome-svg-core';
// 4. Use with `mat-icon` component in your template
@Component({ template: '<button mat-button type="button"><mat-icon svgIcon="font-awesome" style="vertical-align: top"></mat-icon>Make Awesome!</button>' })
export class AppComponent {
constructor(registry: MatIconRegistry, sanitizer: DomSanitizer) {
// 2. Render icon into SVG string
const svg = icon(faFontAwesomeFlag).html.join('');
// 3. Register custom SVG icon in `MatIconRegistry`
registry.addSvgIconLiteral(
'font-awesome',
sanitizer.bypassSecurityTrustHtml(svg)
);
}
}
另请检查此问题以获取更轻量级实现的描述。
方法 2:使用 angular-fontawesome 库中的 fa-icon 组件
由于您似乎已经@fortawesome/angular-fontawesome
在应用程序中使用了 package,因此您可以避免mat-icon
完全使用并使用fa-icon
inside mat-button
。
import { faFontAwesomeFlag } from '@fortawesome/free-brands-svg-icons';
@Component({ template: '<button mat-button type="button"><fa-icon [icon]="faFontAwesomeFlag" style="padding-right: 6px"></fa-icon>Make Awesome!</button>' })
export class AppComponent {
faFontAwesomeFlag = faFontAwesomeFlag;
}
请注意,您还需要添加FontAwesomeModule
到此imports
功能。有关更多详细信息,请参阅文档。
这是两种描述方法的演示:https ://stackblitz.com/edit/components-issue-8znrc5
请注意,我还必须添加一些 CSS 以确保图标与按钮文本很好地对齐。