提问者:小点点

反应-如何使用GlobalStyles为Google字体与情感


我正在使用情感,因为我的css-in-js,我承认,我对它相当陌生。我已经花了1+。一个小时,弄清楚如何注入全球风格,并添加一个谷歌字体,到我的整个应用程序。

我试过:

  • GlobalStyles和@impot(。。)声明。不工作
  • 主题带有情感主题和风格的身体-不工作
  • 链接,在我的index.html和他们的主体{font-family:Roboto}-不工作

谁能给我一个工作的例子,请??我想。在JS中学习CSS,但这变得越来越奇怪。这么多时间,为了这么简单的事。


共1个答案

匿名用户

我认为有两种方法你可以做到:

  • 使用从emotion导出的injectglobal:
import { injectGlobal } from 'emotion'

injectGlobal`
  @font-face {
    font-family: 'Patrick Hand SC';
    font-style: normal;
    font-weight: 400;
    src: local('Patrick Hand SC'),
      local('PatrickHandSC-Regular'),
      url(https://fonts.gstatic.com/s/patrickhandsc/v4/OYFWCgfCR-7uHIovjUZXsZ71Uis0Qeb9Gqo8IZV7ckE.woff2)
        format('woff2');
    unicode-range: U+0100-024f, U+1-1eff,
      U+20a0-20ab, U+20ad-20cf, U+2c60-2c7f,
      U+A720-A7FF;
  }
`
  • 使用global来自@emotion/core:
import { Global, css } from '@emotion/core'

function App() {
  return (
   <div>
    <Global styles={
      css`
        @font-face {
          font-family: 'Patrick Hand SC';
          font-style: normal;
          font-weight: 400;
          src: local('Patrick Hand SC'),
            local('PatrickHandSC-Regular'),
            url(https://fonts.gstatic.com/s/patrickhandsc/v4/OYFWCgfCR-7uHIovjUZXsZ71Uis0Qeb9Gqo8IZV7ckE.woff2)
              format('woff2');
          unicode-range: U+0100-024f, U+1-1eff,
          U+20a0-20ab, U+20ad-20cf, U+2c60-2c7f,
          U+A720-A7FF;
        }
       `
      }
    />
    // Others
   </div>
 )
}