希望你们都做得很好。您能否详细说明为什么会这样。我有一个 ID 为“登录”的按钮。我想当我按下按钮时我想从数据库中获取数据,但我得到一个错误“文档未定义”。我是 js 新手。渴望听到你的消息。
var connection = mysql.createConnection({
host : 'localhost',
user : 'abc',
password : abc,
database : 'abc'
});
connection.connect();
document.getElementById('signin').addEventListener('click', async () => {
Console.log("Hello! ")
})
function newApp() {
const win = new BrowserWindow({ width: 700, height: 400 })
win.loadURL(
url.format({
pathname: "index.html",
slashes: true
})
);
}
最佳答案
Electron 包含两种不同的 JavaScript 环境。 main process and the renderer process 。
Main 进程使用 Node.js 运行并提供对 API 的访问,例如 require
,它提供对 mysql
模块的访问。
在 Chromium 浏览器中运行的渲染器进程提供对典型浏览器 API 的访问,例如 document
对象。
不能在渲染器中使用 MySQL 库,也不能在主进程中使用浏览器的 DOM。
您正在尝试在主进程中使用浏览器的 DOM。
请参阅 quick start guide ,其中说:
The communication between processes is possible via Inter-Process Communication (IPC) modules: ipcMain and ipcRenderer.
如果您想从 MySQL 获取数据到 DOM,您需要使用 IPC 进程将数据从运行在 Main 进程(您现在所在的位置)中的脚本传递到 Renderer 进程(您可以访问
document
目的)。
关于javascript - 文档未定义(使用 electron.js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65752802/