Dynamically get image paths in folder with Nuxt

user1592380

enter image description here

I'm using nuxt with vuetify. I have a working carousel component .I want to generate a list of The .png files in the static folder. Following Dynamically import images from a directory using webpack and Following https://webpack.js.org/guides/dependency-management/#context-module-api my component looks like:

 <template>
  <v-carousel>
    <v-carousel-item v-for="(item,i) in items" :key="i" :src="item.src"></v-carousel-item>
  </v-carousel>
</template>


<script>

  var cache = {};
  function importAll(r) {
    r.keys().forEach(key => cache[key] = r(key));
  }
  var getImagePaths = importAll(require.context('../static/', false, /\.png$/));
  // At build-time cache will be populated with all required modules. 
  export default {
    data: function() {
      return {
        items: getImagePaths
      };
    }
  };
  //     export default {
  //       data() {
  //         return {
  //           items: [{
  //               src: "/52lv.PNG"
  //             },
  //             {
  //               src: "https://cdn.vuetifyjs.com/images/carousel/sky.jpg"
  //             },
  //             {
  //               src: "https://cdn.vuetifyjs.com/images/carousel/bird.jpg"
  //             },
  //             {
  //               src: "https://cdn.vuetifyjs.com/images/carousel/planet.jpg"
  //             }
  //           ]
  //         };
  //       }
  //     };
  //
</script>

I want to search through the static folder and grab the paths to the images , put them in an array and export them to the html template.

I've found that if I edit the script's items array to look the following it will work:

items: [ { src: '/52iv.png' }, { src: '/91Iv.png' }, ....

How can I adjust my code to get the result I need?

EDIT:

I looked at the proposed solution , but after copying it verbatum I got the following error.

enter image description here

user1592380

The following appears to work:

<template>
  <v-carousel>
    <v-carousel-item v-for="(item,i) in items" :key="i" :src="item.src"></v-carousel-item>
  </v-carousel>
</template>


<script>
  var cache = {};
  const images = require.context('../static/', false, /\.png$/);
  var imagesArray = Array.from(images.keys());
  var constructed = [];
  function constructItems(fileNames, constructed) {
    fileNames.forEach(fileName => {
      constructed.push({
        'src': fileName.substr(1)
      })
    });
    return constructed;
  }
  var res = constructItems(imagesArray, constructed);
  console.log(res);
  export default {
    data: function() {
      return {
        items: res
      };
    }
  };

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

dynamically get the image from different resource folder iOS

Get system folder paths in Go?

Get file paths from a folder

Nuxt: displaying local image from static folder

Dynamically get all the paths of the keys of a nested object

Is there a way to scan through folder paths dynamically and list all files?

How to import image dynamically with Nuxt+jQuery script

Get the resource folder path of an image

Get the size of image in a tmp folder

Angular Paths: get file from folder that is on the same directory level

How to get the My Documents folder paths from all users of a machine

How to get a folder size in nodejs with excluding certain paths with glob pattern?

How can I create folder dynamically when moving image?

Get image from a external folder - JAVA - JSF

Get image from resources folder - Laravel

How to get image from assets by folder ios

Get image minimum width within folder

SSIS Get Image from Shared Folder

How to get and save image to folder from listview

how to get the application folder from the docker image

Change background image dynamically from get response

Get Sub Image dynamically using opencv

How to get the image src which is dynamically created

Python get image paths and sizes from a url using BeautifulSoup & PIL

Rails 5.2 API ActiveStorage how to get URL paths for multi image?

Open albumdata.xml with python and get all image paths

How to get spring boot application jar parent folder path dynamically?

Google App Engine Flexible get access to a folder dynamically created by a package?

Downloading file dynamically using caml query only not by get folder method