简体   繁体   English

Vue和打字稿:找不到图片导入

[英]Vue & Typescript: Image imports not found

Problem 问题

I'm currently working on a Vue project using Typescript. 我目前正在使用Typescript进行Vue项目。 Inside my assets folder I have some images I want to import with a separate Typescript file. 在我的Assets文件夹中,我有一些图像想通过单独的Typescript文件导入。


Background 背景

This project is bootstrapped by the vue-cli using Typescript, version 3.2.1 . 该项目由vue-cli使用Typescript版本3.2.1引导。 What I have tried is directly importing into my file using require, which did not work. 我尝试过的是使用require直接导入到我的文件中,该方法不起作用。 I then tried importing with import to no avail 然后我试图与进口import无济于事


Code

This is what I've tried: 这是我尝试过的:

./src/myFile.ts
import Image from '@/assets/img/hello.png';

Does not work 不起作用

./src/myFile.ts
import Image from '../assets/img/hello.png';

Does not work 不起作用

./src/myFile.ts
const Image = require('../assets/img/hello.png');

Does not work 不起作用


Result 结果

The bundler will throw an error along the lines of Relative modules were not found followed by the path of the file. 捆绑器将沿Relative modules were not found的行引发错误,后跟文件路径。 I have made sure that the path is correct and I suspect this has to do with how TypeScript manages imports and how the Vue-cli is configured to bundle my files. 我已经确定路径正确,并且我怀疑这与TypeScript如何管理导入以及Vue-cli如何配置为捆绑文件有关。

Screenshot 截图

Here is a screenshot of the actual project. 这是实际项目的屏幕截图。 The file path is ./src/views/World/Combat/Game.ts : 文件路径是./src/views/World/Combat/Game.ts 项目树截图

Found the issue: You need to declare the following module like so: 找到了问题:您需要像下面这样声明以下模块:

declare module "*.png" {
  const value: string;
  export default value;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM