我对电子有疑问。我正在尝试使用fs在JSON文件中写入数据(例如,添加新类别)。它在开发模式下运行良好,没有任何错误。但是,我使用电子包装程序构建了我的应用程序并运行了它。当我尝试推送数据时,什么也没发生。它甚至都没有给出错误。我刷新/关闭并重新打开它,类别仍然相同,没有任何变化。
这是我的main.js代码的一部分。
ipcMain.on("newCategory", (err, data) =>{ // data is coming from front end with ipcRenderer.send
let href = data.replace(/\s/g,'').toLowerCase();
let myData =[ {
name : data,
href : href
}];
const catFile = fs.readFileSync(path.join(__dirname,"category.json"),'utf8'); // firstly read current file.
const json = JSON.parse(catFile);
json.push(...myData); // push it as a json object
try{
fs.writeFileSync(path.join(__dirname,"category.json"),JSON.stringify(json,null,2)); // write new version of file.
}
catch(e){
console.log(e);
}
});
这也是我的电子包装程序代码
electron-packager . myApp --overwrite --asar --platform=win32 --arch=x64 --icon=assets/icons/myIcon.ico --prune=true --out=release-builds --version-string.CompanyName=CE --version-string.FileDescription=CE --version-string.ProductName="myApp"
此代码有什么问题?为什么它在分发模式下不起作用?我如何使它作为开发模式工作。感谢您的帮助。