1

I'm trying to display all images that exist on a host (server), in one directory (charts) into a vue page instead of having to manually list them all inside of data and therefore having to rebuild with npm every time I want to add an image. The code I have working today is below, any suggestions would be greatly appreciate.

<template>
  <div id="app" class="media-display">
    <img class="media-post" v-for="url of images" :key="url" :src="url + '?rnd=' + cacheKey" />
  </div>
</template>

<script>
import DateTime from './lib/DateTime'
import moment from 'moment'

export default {
  el: '#app',
  data() {
    return {
      images: ['/charts/chart1.png', '/charts/chart1.png'],
      cacheKey: +new Date(),
    }
  },
  created() {
    setInterval(() => {
      this.cacheKey = +new Date()
    }, 10000)
  },
  destroyed() {
    clearInterval(this.interval)
  },
}
</script>
3
  • 1
    1. Expose an API you can hit which returns the list, or 2: Mount the component with the list passed as a property. Commented Aug 15, 2019 at 16:49
  • How could I mount the component with the list passed as a property? Commented Aug 15, 2019 at 17:47
  • If you have a view that renders this component, e.g.: <my-component>, just pass the data to the view, and pass that to the component <my-component :images="images"></my-component> where the images variable was sent to from the server to the view that renders the component. Commented Aug 15, 2019 at 18:54

1 Answer 1

0

You can utilize webpack's require.context() to list files in a given directory at compile time.

https://webpack.js.org/guides/dependency-management/

const illustrations = require.context(
  '@/assets/illustrations',
  true,
  /^.*\.jpg$/
)
console.log(illustrations.keys())

Found in : https://stackoverflow.com/a/58091294/5427882

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.