2

I have a collection of images, audios and videos that should be displayed by a component one by one. All media is placed in assets sub-directories.

Given a simple Vue component for images like:

<template>
  <img :src="src" :alt="src"></a>
</template>

<script>
  export default {
  name: "ImgDisplay",
  props: ['src']
 }
</script>

if I try to use it on some page:

<template>
  <ImgDisplay src="~/assets/test.png"/>
</template>

the image is not actually displayed.

Vue component for MP3-files looks like this:

<template>
  <vue-plyr>
    <audio controls>
      <source :src="src" type="audio/mp3"/>
    </audio>
  </vue-plyr>
</template>

<script>
  export default {
    name: "PlyrAudio",
    props: ['src']
  }
</script>

Then in document I have:

<template>
  <div>
      <article class="infobox">
        <h6>Recording 1</h6>
        <PlyrAudio src="~/assets/media/recording-1.mp3"/>
      </article>
      <article class="infobox">
        <h6>Recording 2</h6>
        <PlyrAudio src="~/assets/media/recording-2.mp3"/>
      </article>
   </div>
</template>

<script>
  import PlyrAudio from "../components/media/PlyrAudio";
  export default {
    name: "PlyrAudioTest",
    components: {PlyrAudio}
  }
</script>

which does not work either, PlyrAudio component does not seem to find referenced mp3 files.

How can one use Nuxt.js aliases (~, ~~, @, @@) in component attributes? Is there some dedicated function to resolve ~ in <script> section of ImgDisplay and PlyrAudio or am I missing something?

dev7
  • 91
  • 2
  • 6
  • If you add that image image to the /static folder it should work with the template: `` automatically. – dlac Mar 13 '19 at 15:59
  • @dlac Your suggestion is fine with a small number of images. The problem is that we have dozens of items in media library, I have updated the question accordingly. We prefer to keep media files in separate sub-directories – dev7 Mar 13 '19 at 17:16
  • Not sure if this will help. But I end up with this solution from this Answer (Solution 2) https://stackoverflow.com/questions/74688433/why-loading-dynamically-assets-fails-on-nuxt-v3 His solution is to prepare the glob with alias ahead and then use it to access the full path later. So that mean you can pass only the image name as props then use the image name combine with the glob to get the full path – Thanyathon Pornsawatchai Jul 25 '23 at 06:10

0 Answers0