我正在try 获取SPA静态应用程序"assets资源 /插图"目录中所有文件的列表,作为JSON对象,以便在Vuex应用store 中重复使用
assets资源 /assets资源 struct 为:
assets
illustrations
ill-1
prop-1-1.jpg
prop-1_2.jpg
ill-2
prop-2-1.jpg
prop-2_2.jpg
ill-3
prop-3-1.jpg
prop-3_2.jpg
我的目标是实现这样的目标:
{ illustrations: [ill-1: [prop-1-1.jpg, prop-1_2.jpg], ill-2: [prop-2-1.jpg, prop-2_2.jpg], ill-3: [prop-3-1.jpg, prop-3_2.jpg]] }
在我的应用程序中.vue,当安装应用程序组件时,我会启动一个方法getIllustrations()
<script>
export default {
name: 'app',
data () {
return {}
},
methods: {
getIllustrations () {
const path = require('path')
const fs = require('fs')
fs.readdir(path.join(__dirname, 'assets/illustrations'), (err, items) => {
if (err) { console.log(err) }
console.log(items)
})
}
},
mounted () {
this.getIllustrations()
}
}
</script>
但我有一个错误:
Error in mounted hook: "TypeError: fs.readdir is not a function"
我还试着在main中运行这个函数.js文件(正在服务器上运行)同样的错误..
我错在哪里?感谢您的反馈