可以使用 Node.js 的 fs 模块来读取文件夹中的 txt 文件内容。具体步骤如下:

  1. 引入 fs 模块
const fs = require('fs');
  1. 使用 fs.readdirSync() 方法获取文件夹中的所有文件名
const folderPath = 'path/to/folder';
const files = fs.readdirSync(folderPath);
  1. 遍历文件名数组,使用 fs.readFileSync() 方法读取每个 txt 文件的内容并存储到一个数组中
const txtContents = [];
for (let i = 0; i < files.length; i++) {
  const fileName = files[i];
  if (fileName.endsWith('.txt')) {
    const filePath = folderPath + '/' + fileName;
    const content = fs.readFileSync(filePath, 'utf-8');
    txtContents.push(content);
  }
}
  1. 最终得到的 txtContents 数组中存储了文件夹中所有 txt 文件的内容,可以在 Vue 组件中使用该数组进行展示。

注意:以上代码中的路径需要根据实际情况进行修改。

Vue 读取文件夹中 TXT 文件内容的步骤

原文地址: https://www.cveoy.top/t/topic/m36Z 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录