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