Home > other >  Vue: Cannot find module (image) in the file
Vue: Cannot find module (image) in the file

Time:11-01

I wrote a line which shows a picture like

enter image description here

And my asset folder is structured as

enter image description here

Yet this is throwing

>Error: Cannot find module '@/assets/img/play.png'" found in
--->  at resources/js/components/MusicPlayer.vue

Solutions I've tried:

  • enter image description here
  • enter image description here
  • enter image description here
  • enter image description here
  • enter image description here
  • enter image description here
  • enter image description here

Tried using enter image description here. Still the same problem.

Any solutions?

CodePudding user response:

Try to create method:

methods: {
  getImgUrl: function (path) { 
     return require('@/assets/img/'   path);
  }
}

and in template call that mathod:

<img :src="getImgUrl('play.png')" />

CodePudding user response:

Your screenshot shows assets under public, but your import URL uses the @ prefix, which is a common alias for <projectRoot>/src.

You can move public/assets to src/assets so that the @/assets URL would be found:

public/assets --move--> src/assets

This solution has the advantage of minimizing the build output, only including files that are actually used in code. The included images also go through Webpack's processing, e.g., if you've configured image optimization.

Alternatively, you can keep public/assets and use a static asset URL, which is the base URL prefixed to the path under public. For the play.img and a base URL of / (the default), the static URL would be /assets/img/play.img:

<img src="/assets/img/play.img">
  • Related