Javascript 电子中的函数不起作用,但节点中的函数起作用
我从Electron和Node运行相同的函数,但得到不同的结果: 从命令行使用Node调用函数效果很好,我得到了连接设备的列表: 节点my_func.js 但是在Electron的main.js中放相同的代码块 (它不在任何其他渲染器文件中,只是使用标准命令行调用:$npm run start), 结果如下: main.jsJavascript 电子中的函数不起作用,但节点中的函数起作用,javascript,node.js,console,electron,instance,Javascript,Node.js,Console,Electron,Instance,我从Electron和Node运行相同的函数,但得到不同的结果: 从命令行使用Node调用函数效果很好,我得到了连接设备的列表: 节点my_func.js 但是在Electron的main.js中放相同的代码块 (它不在任何其他渲染器文件中,只是使用标准命令行调用:$npm run start), 结果如下: main.js const { app, BrowserWindow } = require("electron"); var iosDevice = require(
const { app, BrowserWindow } = require("electron");
var iosDevice = require("node-ios-device");
iosDevice.devices(function (err, devices) {
if (err) {
console.error("Error!", err);
} else {
console.log(devices);
}
});
// SET ENVIRONTMENT
process.env.NODE_ENV = "development";
// process.env.NODE_ENV = "production";
const isDev = process.env.NODE_ENV !== "production" ? true : false;
// CHECK PLATFORM
const isMac = process.platform === "darwin" ? true : false;
let mainWindow;
function createMainWindow() {
mainWindow = new BrowserWindow({
title: "test",
width: 700,
height: 195,
icon: "./assets/icons/Icon_256x256.png",
resizable: isDev ? true : false,
backgroundColor: "white",
show: false,
webPreferences: {
nodeIntegration: true,
// sandbox: true,
// contextIsolation: false,
},
});
mainWindow.loadFile("./app/index.html");
mainWindow.webContents.on("did-finish-load", function () {
mainWindow.show();
});
}
app.on("ready", () => {
createMainWindow();
});
app.on("window-all-closed", () => {
if (!isMac) {
app.quit();
}
});
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createMainWindow();
}
});
- 控制台不显示任何结果
- 显然,创建了三个应用实例,Dock中会弹出三个应用图标:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"start": "electron ."
},
"author": "Viking",
"license": "ISC",
"devDependencies": {
"electron": "^11.4.4"
},
"dependencies": {
"node-ios-device": "^1.9.2"
}
}
要求npm“node-ios-device”软件包并不存在任何问题
你知道我如何用Electron代替Node完成同样的任务吗
多谢各位
更新 在尝试不同的事情之后,这是我最接近的解决方案: 我将函数移动到另一个文件,并从main.js文件调用它,生成一个子进程并使用节点调用函数:
ipcMain.on("dev:getinfo", (event, options) => {
let child_proc;
let devices;
child_proc = spawn("node", ["device.js"]);
child_proc.stdout.on("data", (data) => {
devices = `${data}`;
});
child_proc.on("exit", function (code, signal) {
console.log(devices);
});
});
它在发展中起作用
但我觉得它不会在生产环境中运行,因为我想要发布这个应用程序的用户没有安装Node.js,我不知道如何捆绑electron,这样用户就不需要安装Node.js
因此,一个问题得到了解决,但另一个问题出现了有关我上述评论的更多信息 如果您想从主进程到渲染器(即electron前端或网页)访问IOS设备,请尝试使用 WebContent或ipcMain/ipcRenderer事件发射器
// In the main process.
const { app, BrowserWindow } = require("electron");
const { iosDevice } = require("node-ios-device");
-- previous snips --
let win = null
app.whenReady().then(() => {
win = new BrowserWindow({ width: 800, height: 600 })
win.loadURL(`file://${__dirname}/index.html`)
win.webContents.on('did-finish-load', () => {
let devices = iosDevice.devices(function (err, devices) {
if (err) {
console.error("Error!", err);
} else {
console.log(devices);
return devices;
}
});
win.webContents.send('send-devices', devices);
})
})
然后在index.html中
<!-- index.html -->
<html>
<body>
<script>
require('electron').ipcRenderer.on('send-devices', (event, message) => {
console.log(message) // Prints the devices
})
</script>
</body>
</html>
require('electron').ipcRenderer.on('send-devices',(事件、消息)=>{
console.log(message)//打印设备
})
//您能调试一下,看看我们哪里出了问题吗?请求呼叫失败了吗?是否在禁用节点的渲染器中调用此代码?我们需要看到更多的electron代码谢谢你的回答,main.js文件包含了这个函数,我只是用这个基本命令运行electron:我刚刚更新了原始消息的内容,以增加一点清晰度,谢谢你抱歉的询问。“控制台”是指“浏览器控制台”还是“终端”?main.js中的console.log将不会仅显示在终端的浏览器控制台中。当您调用npm run start时,electron将打开,您可能希望console.log显示在浏览器控制台中。谢谢,我也希望控制台中显示设备列表,因为我从命令行(终端)运行应用程序,所以我可以访问该输出。我尝试了你的建议,但我得到了相同的结果:3个应用程序图标出现在码头和终端上,网络浏览器没有显示任何内容。
<!-- index.html -->
<html>
<body>
<script>
require('electron').ipcRenderer.on('send-devices', (event, message) => {
console.log(message) // Prints the devices
})
</script>
</body>
</html>