簡體   English   中英

Vue和打字稿:找不到圖片導入

[英]Vue & Typescript: Image imports not found

問題

我目前正在使用Typescript進行Vue項目。 在我的Assets文件夾中,我有一些圖像想通過單獨的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如何配置為捆綁文件有關。

截圖

這是實際項目的屏幕截圖。 文件路徑是./src/views/World/Combat/Game.ts 項目樹截圖

找到了問題:您需要像下面這樣聲明以下模塊:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM