简体   繁体   English

FontAwesome 图标在 react/next 应用程序中无法正常工作

[英]FontAwesome Icons not working properly in react/next app

Solved - TLDR;已解决 - TLDR; Adding import '@fortawesome/fontawesome-svg-core/styles.css' to the _app.js / index.js file fixes the issue and FontAwesome works as intended.import '@fortawesome/fontawesome-svg-core/styles.css'添加到 _app.js / index.js 文件可以解决问题,并且 FontAwesome 可以按预期工作。 My issue was caused by npx-create-next-app including purgeCSS by default, which in turn stripped out the FontAwesome required styles.我的问题是由默认情况下包含 purgeCSS 的 npx npx-create-next-app引起的,这反过来又删除了 FontAwesome 所需的 styles。


I'm using FontAwesome in my Next app.我在我的 Next 应用程序中使用 FontAwesome。 I followed the React guide on the FA website and the icon SVG's are being output on the page.我遵循了 FA 网站上的React 指南,页面上的图标 SVG 是 output。 Problem is, none of the features work and they don't scale with font-size as they're meant to.问题是,这些功能都不起作用,并且它们不能按照预期的字体大小进行缩放。

I don't want to hack it together by manually targeting the SVG's and adding size etc. as it's not ideal when it comes to responsiveness.我不想通过手动定位 SVG 并添加大小等来破解它,因为它在响应性方面并不理想。 ie it would be nice to have icons scale with accompanying text and the ability to add 'spinner', 'fixedWidth' etc.即,如果图标与随附的文本一起缩放并且能够添加“微调器”、“固定宽度”等,那就太好了。

Strangely, they have started working once or twice but then break again and I can't seem to reproduce.奇怪的是,他们已经开始工作一两次,但后来又中断了,我似乎无法重现。

// package.json
"dependencies": {
    "@fortawesome/react-fontawesome": "^0.1.14",
    "@fortawesome/fontawesome-svg-core": "^1.2.34",
    "@fortawesome/pro-regular-svg-icons": "^5.15.2",
  }
// _app.js
import { library } from '@fortawesome/fontawesome-svg-core'
import { faHeart } from '@fortawesome/pro-regular-svg-icons'
library.add( faHeart )
// header.js
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'

export default function Header() {

  return (
    <header>
      <FontAwesomeIcon icon={['far', 'heart']} spin />
    </header>
  )
}
// style.css
header {
 font-size: 20px; (does nothing to the icon)
}

svg {
 width: 20px; (works, but this shouldn't be required according to FA docs)
}

I've also tried individual use (importing icons into individual components, rather than utilising the library function) to the same effect.我也尝试过单独使用(将图标导入单个组件,而不是利用库函数)达到相同的效果。 Like so:像这样:

// header.js
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faHeart } from '@fortawesome/pro-regular-svg-icons'

export default function Header() {

  return (
    <header>
      <FontAwesomeIcon icon={faHeart} spin />
    </header>
  )
}

Sounds like it's something to do with purgeCSS which is used by default when using create-next-app which strips out the required CSS.听起来这与 purgeCSS 有关,它在使用create-next-app时默认使用,它会去除所需的 CSS。

Adding import '@fortawesome/fontawesome-svg-core/styles.css' fixes the issue and FontAwesome works as intended.添加import '@fortawesome/fontawesome-svg-core/styles.css'解决了这个问题,并且 FontAwesome 可以按预期工作。 In my case, I added this to the top of _app.js就我而言,我将其添加到 _app.js 的顶部

I use FontAwesomeIcon in my React apps like this and it works:我在这样的 React 应用程序中使用 FontAwesomeIcon 并且它可以工作:

import { faHeart} from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";

in the code:

<FontAwesomeIcon  className="icon" icon={faHeart} />
                        

and in css:在 css 中:

.icon{
color: ; / if you want to change color
font-size: 36px;
}

Essentially, all you need to do is:本质上,您需要做的就是:

import the icon:导入图标:

import { yourIcon} from "@fortawesome/free-solid-svg-icons";

and use it:并使用它:

<FontAwesomeIcon icon={yourIcon} />

You can add a classname to the icon and use that class to style it.您可以为图标添加一个类名并使用该 class 来设置它的样式。

<FontAwesomeIcon icon={yourIcon} className="styled-icon" />

Here is a good video on adding font awesome icons to next.js: https://youtu.be/kaA2aX4X3NU这是一个关于向 next.js 添加字体真棒图标的好视频: https://youtu.be/kaA2aX4X3NU

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM