我正在制作一个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>

我已经使用了上面HERE页的脚本.

请帮忙,谢谢.


EDIT:下面是@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>

推荐答案

从网页文件:

传递给require.context的参数必须是文字!

所以我认为在你的例子中,解析require.context次失败了.

try 传递文本而不是imageDir变量

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

这是必要的,因为webpack无法在绑定期间解析您的运行时变量值.

Vue.js相关问答推荐

Pinia+Vue 3状态react 性-StoreToRef的替代方案

Vuetify/Vue3 插槽模板之间的阴影

Vue 3组合式API如何测试是否发出正确的事件

vue 不重新渲染数据更改

如何在 vue2.7 中删除 slot-scope

如何在vue中获取特定类别的产品列表

在 Ajax 函数中访问 Vue.js 组件属性

VueJS CKeditor5 上传图片

使用带有 v-date-picker 的 new Date() 不起作用

Vuetify Datatable - 在所有列上启用内容编辑

将数据传递给另一条路由上的组件

Modal 内部的 Vue.js AJAX 调用

Vuejs:我应该在 vue-router SPA 中的哪里放置一些常用的 js 工具?

如何异步验证 Vuetify 文本字段?

异步方法不等待函数

了解 Nuxt.js 中的State状态和Getters:Getters不起作用

Vue CLI 3 sass-resources-loader - Options.loaders 未定义

在 Vue JS 组件中使用 Laravel 路由的最佳方法是什么

Vue中的嵌套循环

使用 Vee-validate 禁用按钮,直到正确填写表单