从电子网络应用程序写入 JSON 文件
Writing to a JSON file from an electron web app
我使用 electron 制作了一个应用程序,可以生成 6 个数组来创建动画帧,这些数组将由另一个程序解释以在 6x6x6 led 立方体上显示动画。
我希望能够将这些数组添加到 JSON 文件中。
我已经尝试将 const fs = require('fs');
添加到 js 文件并使用它来添加它以添加到 JSON 文件,但是它之后的所有内容都没有 运行 并且没有显示错误。
有没有办法解决 fs 的问题,或者有其他方法可以解决这个问题?
(我启用了节点集成)
main.js
const url = require("url");
const path = require("path");
const { Menu } = require("electron");
const {app, BrowserWindow} = electron;
let mainwindow;
app.on("ready", function(){
mainwindow = new BrowserWindow({
webPreferences: {
nodeIntegration: true, // <--- flag
nodeIntegrationInWorker: true // <--- for web workers
}
});
mainwindow.loadURL(url.format({
pathname: path.join(__dirname, "Index.html"),
protocol: "file:",
slashes: true,
}));
const mainMenu = Menu.buildFromTemplate(mainMenuTemplate);
Menu.setApplicationMenu(mainMenu);
mainwindow.maximize();
});
const mainMenuTemplate = [
{
label:"File",
submenu:[
{
label:"Upload",
},
{
label:"Clear Animation"
}
]
}
];
Index.js
const fs = require('fs');
var L1 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var L2 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var L3 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var L4 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var L5 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var L6 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
function CompileButton(){
document.getElementById("Output").innerText = "int Layer1=[" + L1 + "]\nint Layer2=[" + L2 +"]\nint Layer3=[" + L3 + "]\nint Layer4=[" + L4 + "]\nint Layer5=[" + L5 + "]\nint Layer6=[" + L6 + "]";
}
compileButton.addEventListener("click", () => {
CompileButton();
var frame = {
"layers": [
L1,
L2,
L3,
L4,
L5,
L6
]
};
var obj = JSON.stringify(frame);
});
要解决此问题,您需要做的就是禁用 Web 首选项中的上下文隔离以及启用节点集成,如下所示:
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
}
我在这里找到了答案:
我使用 electron 制作了一个应用程序,可以生成 6 个数组来创建动画帧,这些数组将由另一个程序解释以在 6x6x6 led 立方体上显示动画。
我希望能够将这些数组添加到 JSON 文件中。
我已经尝试将 const fs = require('fs');
添加到 js 文件并使用它来添加它以添加到 JSON 文件,但是它之后的所有内容都没有 运行 并且没有显示错误。
有没有办法解决 fs 的问题,或者有其他方法可以解决这个问题?
(我启用了节点集成)
main.js
const url = require("url");
const path = require("path");
const { Menu } = require("electron");
const {app, BrowserWindow} = electron;
let mainwindow;
app.on("ready", function(){
mainwindow = new BrowserWindow({
webPreferences: {
nodeIntegration: true, // <--- flag
nodeIntegrationInWorker: true // <--- for web workers
}
});
mainwindow.loadURL(url.format({
pathname: path.join(__dirname, "Index.html"),
protocol: "file:",
slashes: true,
}));
const mainMenu = Menu.buildFromTemplate(mainMenuTemplate);
Menu.setApplicationMenu(mainMenu);
mainwindow.maximize();
});
const mainMenuTemplate = [
{
label:"File",
submenu:[
{
label:"Upload",
},
{
label:"Clear Animation"
}
]
}
];
Index.js
const fs = require('fs');
var L1 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var L2 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var L3 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var L4 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var L5 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var L6 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
function CompileButton(){
document.getElementById("Output").innerText = "int Layer1=[" + L1 + "]\nint Layer2=[" + L2 +"]\nint Layer3=[" + L3 + "]\nint Layer4=[" + L4 + "]\nint Layer5=[" + L5 + "]\nint Layer6=[" + L6 + "]";
}
compileButton.addEventListener("click", () => {
CompileButton();
var frame = {
"layers": [
L1,
L2,
L3,
L4,
L5,
L6
]
};
var obj = JSON.stringify(frame);
});
要解决此问题,您需要做的就是禁用 Web 首选项中的上下文隔离以及启用节点集成,如下所示:
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
}
我在这里找到了答案: