electron-vue 本地文件读取与写入

const fs = require("fs");
const path = require("path");
const { ipcRenderer } = require("electron");
  data() {
    return {
      saveData: []
    };
  },

 

    // 读取本地配置文件
    readBtnToFile() {
      // 读取本地配置json文件
      const File_path = path
        .join(path.resolve("./../"), "/cabinetMachineList.json")
        .replace(/\\/g, "\/");
      const config_res = JSON.parse(fs.readFileSync(File_path, "utf-8"));

      fs.readFile(File_path, "utf8", (err, data) => {
        if (err) {
          this.$message({
            message: "文件读取失败!",
            type: "error",
            offset: 100,
          });
        } else {
          let list = JSON.parse(data); 
          console.log(data, list);
          this.saveData = list;
          // this.$message({
          //   message: "文件读取成功,并已自动同步!",
          //   type: "success",
          //   offset: 100,
          // });
        }
      });
    },
    // 保存文件
    saveBtnToFile() {
      // 获取本地配置json文件
      const File_path = path
        .join(path.resolve("./../"), "/cabinetMachineList.json")
        .replace(/\\/g, "\/");
      fs.writeFile(File_path, JSON.stringify(this.saveData), "utf8", (err) => {
        if (err) {
          this.$message({
            message: "文件写入失败!",
            type: "error",
            offset: 100,
          });
        } else {
          this.$root.cabinetMachineList = this.saveData;
          this.$message({
            message: "文件写入成功!",
            type: "success",
            offset: 100,
          });
        }
      });
    },

你可能感兴趣的:(vue.js,electron,javascript)