css - React - 如何将 GlobalStyles 用于带有情感的 Google 字体
问题描述
我正在使用 Emotion,我css-in-js
和我都承认,我对它还很陌生。我花了1+。小时,弄清楚如何在我的整个应用程序中注入全局样式并添加谷歌字体。
我试过了:
- 和
globalStyles
@impot(..) 语句。不工作 theme
搭配和风格的emotion-theming
身体 - 不工作- 链接,在我
index.html
和他们的 body{font-family: Roboto} - 不工作
有人可以给我一个工作的例子吗?我想要。在 JS 中学习 CSS,但这变得很奇怪。这么多时间,为了这么简单的事情。
解决方案
我认为有两种方法可以做到:
- 使用
injectGlobal
导出自emotion
:
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>
)
}
推荐阅读
- javascript - create-react-app 不拾取 .env 文件
- javascript - 如何在 GraphQL 中正确链接 useQuery 和 useMutation?
- iis - 网站主页错误,但 Default.aspx 正在运行
- c# - 对路径的访问被拒绝到虚拟目录的物理路径
- c# - 如何处理 Xamarin.forms webview 中的加载错误
- python - SyntaxError 剩余/在 py 文件中的行之间移动
- jwt - Sequelize - 模型设置器方法不起作用,我没有收到任何错误
- woocommerce - WooCommerce 中是否有办法在产品类别显示之后但在产品之前显示自定义内容?
- c++ - 设计一个带有两个指向嵌套对象的指针的迭代器
- c# - 使用 safebrowsinglookup 库的 GoogleSafeBrowsing api