electron之总结

  1. electron 文件导入库文件报错问题,原因:加载不到对应库。所以需要修改依赖加载层级 。方法如下

module.paths.push
module.paths.unshift

在文件开头加入一行如下

module.paths.push(process.env.NODE_PATH);

or

module.paths.unshift('/usr/lib/node_modules');
  1. 禁止请求 HTTP 时使用磁盘缓存.(否则页面加载的渲染css,js都不是最新的)

–disable-http-cache

使用方法:

app.commandLine.appendSwitch("--disable-http-cache");
  1. 桌面端隐藏到托盘
mainWindow.setSkipTaskbar(true);
  1. window 加载url或者本地文件
    本地文件
let localPath_error = 'file://'+__dirname+'/error.html';
mainWindow.loadURL(localPath_error );

请求远程地址

mainWindow.loadURL('https://www.baidu.com');
  1. 关于electron4.3 升级到electron5.0.3 版本,服务器js中代码报错:Uncaught ReferenceError: require is not defined
    electron中ipcrenderer不工作,传值不成功的问题。官方说明:在启用Node.js集成的情况下,你都不该加载并执行远程代码

解决方法:在客服端代码中创建mainWindow ,注意contextIsolation和nodeIntegration参数

    const windowOptions = {
            width: 1000,
            minWidth: 680,
            height: 900,
            minHeight:360,
            title: app.getName(),
            autoHideMenuBar: true,
            webPreferences: {nodeIntegration:true,contextIsolation:false}
        };

        mainWindow = new BrowserWindow(windowOptions);
  1. 关于electron 单例
const shouldQuit = app.requestSingleInstanceLock((commandLine, workingDirectory) => {
  if (mainWindow) {
      console.log("commandLine",commandLine);
    if (mainWindow.isMinimized()) mainWindow.restore();
    mainWindow.focus()
  }else {
      console.log("commandLine",commandLine);

      initialize()
  }
});
      console.log("shouldQuit+++",shouldQuit);

if (!shouldQuit) {
  app.quit()
}else {
    initialize()
}
  1. 关于elctron调用nodejs 执行命令

child_process即子进程可以创建一个系统子进程并执行shell命令,在与系统层面的交互上挺有用处
熟悉shell脚本的同学,可以用它来完成很多有意思的事情,比如文件压缩、增量部署,远程计算机,更新文件等等
几种创建子进程的方式

注意事项:
下面列出来的都是异步创建子进程的方式,每一种方式都有对应的同步版本。

  • .exec()、.execFile()、.fork()底层都是通过.spawn()实现的。
  • .exec()、execFile()额外提供了回调,当子进程停止的时候执行。
  • child_process.spawn(command, args)
  • child_process.exec(command, options)
  • child_process.execFile(file, args[, callback])
  • child_process.fork(modulePath, args)
  • child_process.exec(command, options)

创建一个shell,然后在shell里执行命令。执行完成后,将stdout、stderr作为参数传入回调方法。
举例:

shell = 'ipconfig /renew';
    exec(shell,function (error,stdout,stderr) {
        tray.displayBalloon({title:"请重试", content:"请重新启动导航台尝试" });
    });
  1. 要正确使用on 和once 的监听机制
 workerProcess.stdout.on('data', function (data) {
        createWindow("https://www.baidu.com");

        mainWindow.webContents.once('did-finish-load', function () {
            console.log("did-finish-load:", data);
            // 主进程发送消息
            mainWindow.webContents.send('loginUserData', data);
        });

    });
  1. 待续……
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值