我正在尝试在Vue浏览器应用程序和Electron主进程之间建立通信,但它没有起作用。在调用
主进程包含侦听器,然后进行文件处理。
startBot()
之前,我就收到一个错误消息,说__Dirname
未知。但是这个常量在代码中找不到。我做错了什么?这是我打开带有预加载的浏览器窗口的background.js文件。这样做的目的是使窗口可用于浏览器进程。请参考链接:https://gist.github.com/Quenos/7d6dbe4f5410739499ea9e3b0b4f961a.js。function createWindow() {
// Create the browser window.
win = new BrowserWindow({
width: 1300,
height: 1100,
title: "Hedgehog TRDR Bot",
icon: path.join(__static, "hedgehog.jpg"),
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
enableRemoteModule: false,
// __static is set by webpack and will point to the public directory
preload: path.resolve(__static, "preload.js"),
},
});
这是preload.js
const { contextBridge, ipcRenderer } = require("electron");
const validChannels = ["READ_FILE", "WRITE_FILE"];
contextBridge.exposeInMainWorld("ipc", {
send: (channel, data) => {
if (validChannels.includes(channel)) {
ipcRenderer.send(channel, data);
}
},
on: (channel, func) => {
if (validChannels.includes(channel)) {
// Strip event as it includes `sender` and is a security risk
ipcRenderer.on(channel, (event, ...args) => func(...args));
}
},
});
主进程包含侦听器,然后进行文件处理。
const { ipcMain } = require("electron");
const fs = require("fs");
var file;
ipcMain.on("OPEN_FILE", (event, payload) => {
console.log("daaro");
file = fs.createWriteStream(payload.path);
event.reply("OPEN_FILE", { content: "roger" });
});
ipcMain.on("TEST_FILE", (event, payload) => {
console.log("daaro");
file.write(payload.path);
event.reply("OPEN_FILE", { content: "roger" });
});
还有发送文件处理请求的浏览器进程:
async startBot() {
window.ipc.send("OPEN_FILE", { path: "./HH_trdr_bot.csv" });
}
TypeError: window.ipc.on is not a function
,你是怎么解决的?@MPL,@user3067684 - Saqib Shehzad