reactjs - Font Awesome 图标不会出现在 DOM 中
问题描述
在我的数据库中,我有这些图标,如字符串 STRING 类型 毕业帽或眼睛等图标通常出现在页面上,但其他图标没有。
const CourseIcon = ({
courseType: { type, prefix, custom, faIcon, icon },
className,
...rest
}) => {
return custom ? (
<img
src={require(`../../img/course-icons/${icon}.png`)}
className={classnames('course-icon', className)}
title={type}
alt={type}
/>
) : (
<FontAwesomeIcon
className={classnames('course-icon', className)}
title={type}
icon={faIcon}
{...rest}
/>
);
};
CourseIcon.propTypes = {
courseType: PropTypes.object.isRequired,
};
export default CourseIcon;
这是代码。当我在 courseType 上使用 console.log 时,所有内容都已定义。
解决方案
新的 font-awesome 将图标样式拆分为几个包。安装所需的图标并将图标从它们正确导入项目后,您还必须在使用带有FontAwesomeIcon
.
例如:
导入和库定义:
// Notice that this icon comes from the regular style package.
import { faKeyboard } from '@fortawesome/free-regular-svg-icons';
import { library } from '@fortawesome/fontawesome-svg-core';
library.add(faKeyboard);
然后,在您的组件内部:
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
// ... other code parts
const YourComponent = () => {
return (
// The first element of the icon array will define your icon style
// far = font awesome regular (obviously)
// The rest of it will define the icon itself
<FontAwesomeIcon icon={['far', 'keyboard']} />
);
};
推荐阅读
- jquery - 我想使用 jquery 更改语言 url
- c++ - 如何在 boost::beast 上保持 websocket?
- javascript - 根据另一组元素的类别不断隐藏不同的元素
- python - 是否可以从 Jenkins 执行 Jupyter Notebook 文件并向其返回值?
- php - Wordpress 自定义设置返回 10NULL
- python - 在 C++ 中获取 Python 函数参数名称
- java - 如何将以下代码冒泡排序为多维数组?
- assembly - 在 DOSBox 中打开文件句柄会清除文件的数据
- javascript - 查找对象中没有值、空或 null 的所有元素
- android - 如何在 tablayout 中查看选项卡?不是片段的视图