如何使用vue.js / nuxt.js获取目录中的所有图像文件

JavaScript Vue.js

Harry凯

2020-03-27

我正在一个nuxt.js项目上并出现错误:

在浏览器中,我看到此错误:

__webpack_require__(...).context is not a function

而且,在终端中,我收到此错误:

Critical dependency: require function is used in a way in which dependencies cannot be statically extracted 

这是我的代码

<script>
export default {
  name: 'SectionOurClients',
  data() {
    return {
      imageDir: '../assets/images/clients/',
      images: {},
    };
  },

  mounted() {
    this.importAll(require.context(this.imageDir, true, /\.png$/));
  },

  methods: {
    importAll(r) {
      console.log(r)
    },
  },
};
</script>

我已经从使用上面的脚本这里

请帮忙,谢谢。


编辑:遵循@MaxSinev的答案后,这是我的工作代码的外观:

<template lang="pug">
  .row
    .col(v-for="client in images")
      img(:src="client.pathLong")
</template>

<script>
export default {
  name: 'SectionOurClients',
  data() {
    return {
      images: [],
    };
  },

  mounted() {
    this.importAll(require.context('../assets/images/clients/', true, /\.png$/));
  },

  methods: {
    importAll(r) {
      r.keys().forEach(key => (this.images.push({ pathLong: r(key), pathShort: key })));
    },
  },
};
</script>

第3817篇《如何使用vue.js / nuxt.js获取目录中的所有图像文件》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

0个回答

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android