我目前正在使用Typescript进行Vue项目。在我的资产文件夹中,我有一些图像想通过单独的Typescript文件导入。
该项目由vue-cli使用Typescript版本3.2.1
引导。我尝试过的是使用require直接导入到我的文件中,该方法不起作用。然后,我尝试使用import
进行导入无济于事
这是我尝试过的:
./src/myFile.ts
import Image from '@/assets/img/hello.png';
不起作用
./src/myFile.ts
import Image from '../assets/img/hello.png';
不起作用
./src/myFile.ts
const Image = require('../assets/img/hello.png');
不起作用
捆绑程序将在Relative modules were not found
行中引发错误,并在文件路径后引发错误。我已经确定路径正确,并且我怀疑这与TypeScript如何管理导入以及Vue-cli如何配置为捆绑文件有关。
答案 0 :(得分:0)
发现了问题:您需要像下面这样声明以下模块:
declare module "*.png" {
const value: string;
export default value;
}