use*_*629 9 javascript webpack vue.js nuxt.js nuxt
我正在使用带有vuetify的nuxt.我有一个工作轮播组件.我想生成静态文件夹中的.png文件列表.以下使用webpack从目录动态导入图像,然后按照https://webpack.js.org/guides/dependency-management/#context-module-api我的组件如下所示:
<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>
Run Code Online (Sandbox Code Playgroud)
我想搜索静态文件夹并获取图像的路径,将它们放在一个数组中并将它们导出到html模板.
我发现如果我编辑脚本的items数组来查看以下内容它将起作用:
items:[{src:'/52iv.png'},{src:'/ 91Iv.png'},....
如何调整代码以获得我需要的结果?
编辑:
我查看了建议的解决方案,但在复制verbatum之后,我收到了以下错误.
以下似乎有效:
<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
};
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2382 次 |
| 最近记录: |