typescript - 将本地字体 .otf 文件导入打字稿项目时找不到模块错误
问题描述
我正在尝试将本地字体导入我的 React/Typescript 项目。
字体文件 ,Averta-Cyrillic_Regular.otf
放置在src/assets/fonts/
文件夹中。
在同一个fonts.d.ts
文件夹中是一个只有一行的文件:
declare module '*.otf'
我的tsconfig.json
文件具有以下包含规则:
"include": ["./src/**/*.ts", "./src/**/*.tsx"]
那么从技术上讲,它应该可以正常导入吗?
但是,当我尝试src/assets/fonts/fonts.ts
像下面这样导入字体时:
import { createGlobalStyle } from 'styled-components'
import AvertaCyrillicRegular from './Averta-Cyrillic_Regular.otf'
export default createGlobalStyle`
@font-face {
font-family: 'Averta Cyrillic Regular';
src: local('Averta Cyrillic Regular'), local('AvertaCyrillicRegular'),
url(${AvertaCyrillicRegular}) format('otf'),
font-weight: 300;
font-style: normal;
},
`
它抛出以下错误:
Cannot find module './Averta-Cyrillic_Regular.otf' or its corresponding type declarations. TS2307
1 | import { createGlobalStyle } from 'styled-components'
2 |
> 3 | import AvertaCyrillicRegular from './Averta-Cyrillic_Regular.otf'
解决方案
只需在我们项目的 .d.ts 文件中添加一个带有字体文件扩展名的声明。
declare module "*.woff2"
推荐阅读
- java - 在 IOS 模块(libGDX)中实现 AdMob 奖励视频广告
- azure-devops - 如何从发布任务修改 Azure DevOps 发布定义变量?
- javascript - 使用 JQuery 删除表行自动计算总值
- java - Python客户端Java服务端接收null
- javascript - 将数组 2 和 3 访问到一个字符串数组中
- python - 使用 Python 和 FFMPEG 的应用程序文件
- ui-automation - 用于跨平台自动化应用程序鼠标、键盘事件的 Python 库
- java - 如何通过 java 向 Cisco/Alcatel 交换机运行“显示接口状态”(如果可能,请使用 jsch)
- performance - AVX mat4 inv 实现比 SSE 慢
- powershell - 从批处理文件在 Powershell ISE 中运行脚本