问题描述
angular 项目,需要引入字体图标,在项目初始化的时候注册了字体,如下:
this.iconLibrary.registerFontPack('iconfont', { iconClassPrefix: 'icon' });
使用nb-icon 调用字体图标,如下:
<nb-icon icon="" pack="iconfont"></nb-icon>
<span>控制面板</span>
结果渲染成了框框,说明字体识别不出来。
问题解决
其中packClass
属性至关重要,用于指明字体图标的font-family
this.iconLibrary.registerFontPack('iconfont', { packClass: 'iconfont', iconClassPrefix: 'icon' });
核心代码如下:
export class NbFontIcon {
constructor(name, content, params = {}) {
this.name = name;
this.content = content;
this.params = params;
}
getClasses(options) {
const classes = [];
if (this.params.packClass) {
classes.push(this.params.packClass);
}
const name = this.params.iconClassPrefix ? `${this.params.iconClassPrefix}-${this.name}` : this.name;
classes.push(name);
return classes;
}
getContent(options) {
return this.content;
}
}