Dynamically get image paths in folder with Nuxt












7















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










share|improve this question

























  • Does this help?

    – Islam Elshobokshy
    Jan 3 at 15:32











  • Thank you I tried it verbatim but got the following error - Please see edit

    – user61629
    Jan 3 at 21:29











  • are you sure your path is correct? what happens if you do '../main/' or '~/main/ instead? in regards to your original code. it looks like your carousel component is only one folder deep.

    – ryeMoss
    Jan 4 at 1:01













  • @ryeMoss , I've changed the folder to static

    – user61629
    Jan 4 at 18:43











  • I believe I was able to get it working using require.context() and the files in /main/. I imagine it would work the same in /static/. Unfortunately I'm also not very familiar with webpack to help out with your new method

    – ryeMoss
    Jan 4 at 18:54
















7















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










share|improve this question

























  • Does this help?

    – Islam Elshobokshy
    Jan 3 at 15:32











  • Thank you I tried it verbatim but got the following error - Please see edit

    – user61629
    Jan 3 at 21:29











  • are you sure your path is correct? what happens if you do '../main/' or '~/main/ instead? in regards to your original code. it looks like your carousel component is only one folder deep.

    – ryeMoss
    Jan 4 at 1:01













  • @ryeMoss , I've changed the folder to static

    – user61629
    Jan 4 at 18:43











  • I believe I was able to get it working using require.context() and the files in /main/. I imagine it would work the same in /static/. Unfortunately I'm also not very familiar with webpack to help out with your new method

    – ryeMoss
    Jan 4 at 18:54














7












7








7


1






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










share|improve this question
















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







javascript vue.js webpack nuxt.js nuxt






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 8 at 15:08







user61629

















asked Jan 1 at 5:41









user61629user61629

9,11436137269




9,11436137269













  • Does this help?

    – Islam Elshobokshy
    Jan 3 at 15:32











  • Thank you I tried it verbatim but got the following error - Please see edit

    – user61629
    Jan 3 at 21:29











  • are you sure your path is correct? what happens if you do '../main/' or '~/main/ instead? in regards to your original code. it looks like your carousel component is only one folder deep.

    – ryeMoss
    Jan 4 at 1:01













  • @ryeMoss , I've changed the folder to static

    – user61629
    Jan 4 at 18:43











  • I believe I was able to get it working using require.context() and the files in /main/. I imagine it would work the same in /static/. Unfortunately I'm also not very familiar with webpack to help out with your new method

    – ryeMoss
    Jan 4 at 18:54



















  • Does this help?

    – Islam Elshobokshy
    Jan 3 at 15:32











  • Thank you I tried it verbatim but got the following error - Please see edit

    – user61629
    Jan 3 at 21:29











  • are you sure your path is correct? what happens if you do '../main/' or '~/main/ instead? in regards to your original code. it looks like your carousel component is only one folder deep.

    – ryeMoss
    Jan 4 at 1:01













  • @ryeMoss , I've changed the folder to static

    – user61629
    Jan 4 at 18:43











  • I believe I was able to get it working using require.context() and the files in /main/. I imagine it would work the same in /static/. Unfortunately I'm also not very familiar with webpack to help out with your new method

    – ryeMoss
    Jan 4 at 18:54

















Does this help?

– Islam Elshobokshy
Jan 3 at 15:32





Does this help?

– Islam Elshobokshy
Jan 3 at 15:32













Thank you I tried it verbatim but got the following error - Please see edit

– user61629
Jan 3 at 21:29





Thank you I tried it verbatim but got the following error - Please see edit

– user61629
Jan 3 at 21:29













are you sure your path is correct? what happens if you do '../main/' or '~/main/ instead? in regards to your original code. it looks like your carousel component is only one folder deep.

– ryeMoss
Jan 4 at 1:01







are you sure your path is correct? what happens if you do '../main/' or '~/main/ instead? in regards to your original code. it looks like your carousel component is only one folder deep.

– ryeMoss
Jan 4 at 1:01















@ryeMoss , I've changed the folder to static

– user61629
Jan 4 at 18:43





@ryeMoss , I've changed the folder to static

– user61629
Jan 4 at 18:43













I believe I was able to get it working using require.context() and the files in /main/. I imagine it would work the same in /static/. Unfortunately I'm also not very familiar with webpack to help out with your new method

– ryeMoss
Jan 4 at 18:54





I believe I was able to get it working using require.context() and the files in /main/. I imagine it would work the same in /static/. Unfortunately I'm also not very familiar with webpack to help out with your new method

– ryeMoss
Jan 4 at 18:54












1 Answer
1






active

oldest

votes


















1














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
};
}
};







share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993252%2fdynamically-get-image-paths-in-folder-with-nuxt%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    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
    };
    }
    };







    share|improve this answer




























      1














      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
      };
      }
      };







      share|improve this answer


























        1












        1








        1







        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
        };
        }
        };







        share|improve this answer













        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
        };
        }
        };








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 10 at 1:36









        user61629user61629

        9,11436137269




        9,11436137269
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993252%2fdynamically-get-image-paths-in-folder-with-nuxt%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith