我已经在tsconfig.json
中配置了paths
。一切工作正常,除了导入资产文件如svg
和png
。
这是我的paths
在tsconfig.json
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@components/*": ["./src/components/*"],
"@components": ["./src/components"],
"@app/*": ["./src/app/*"],
"static": ["static/*"]
},
...
}
下面是我如何导入:
import imageAdd from "static/images/image-add.png";
import circleIcon from "static/icons/circle-close.svg"
将"static": ["static/*"]
替换为"static/*": ["static/*"]
终于解决了。需要添加一些d.ts
文件。如。images.d.ts
with content:
declare module "*.png";
declare module "*.jpg";
转到tsconfig.json
文件并将此文件添加到include
:
"include": ["images.d.ts", <other includes>]
你可能需要安装file-loader
。我使用gatsby,所以我不需要配置任何东西