wuyuedefeng / blogs

博客文章在issue中
5 stars 0 forks source link

electron-vue 使用常见问题总结 #40

Open wuyuedefeng opened 4 years ago

wuyuedefeng commented 4 years ago

编译app

$ npm run mac
$ npm run win32

Mac下编译exe

$ brew cask install xquartz
$ brew cask install wine-stable
$ npm run win32

无法正常使用一些node库,需重新编译

electron node 版本不匹配的解决细节, node-ffi-func无法使用node插件相关问题

yarn add electron-rebuild --dev
./node_modules/.bin/electron-rebuild

数据库

nedb


下载

build时候无法下载,或者下载缓慢electron

downloading     parts=4 size=42 MB url=https://github.com/electron/electron/releases/download/v2.0.18/electron-v2.0.18-win32-ia32.zip

更改镜像源electron为淘宝源

npm config set ELECTRON_MIRROR https://npmmirror.com/mirrors/electron/

webview和主进程之间数据通信

<template>
  <webview :src="webviewSrc" :preload="preloadFile"></webview>
</template>

<script>
export default {
  data () {
    return {
      webviewSrc: this.$utils.taobao.urls.login,
      preloadFile: `file://${path.join(__static, '/taobao/login.js')}`,
    }
  }
}
</script>
// 在页面
const webview = document.querySelector('webview')
webview.addEventListener('ipc-message', (event) => {
  console.log(event.channel)
  // Prints "pong"
})
webview.send('ping', '我是消息体')
// webview.send必须等页面加载完后再调用
// webview.addEventListener('did-finish-load', () => {
//  this.webview.send('continue')
// })

// 在webview
const {ipcRenderer} = require('electron')
  ipcRenderer.on('ping', (event, message) => {
  ipcRenderer.sendToHost('pong')
})
wuyuedefeng commented 4 years ago

electron中文资料:

electron与c++/.net通信 让electron-app支持带参数的命令行调用 环信实操 | 使用electron进行跨平台桌面开发 在electron中调用C++动态库的经验总结

直接调用系统动态库

  • demo
    
    var ref = require('ref');
    var ffi = require('ffi');

var voidPtr = ref.refType(ref.types.void); var stringPtr = ref.refType(ref.types.CString);

var user32 = ffi.Library('user32.dll', { EnumWindows: ['bool', [voidPtr, 'int32']], GetWindowTextA : ['long', ['long', stringPtr, 'long']] });

windowProc = ffi.Callback('bool', ['long', 'int32'], function(hwnd, lParam) { var buf, name, ret; buf = new Buffer(255); ret = user32.GetWindowTextA(hwnd, buf, 255); name = ref.readCString(buf, 0); console.log(name); return true; });

user32.EnumWindows(windowProc, 0);