项目作者: zero9527

项目描述 :
react-ts-antd-template 的 Electron
高级语言: JavaScript
项目地址: git://github.com/zero9527/react-ts-antd-template-electron.git


react-ts-antd-template-electron

  1. git clone https://github.com/zero9527/react-ts-antd-template-electron.git
  2. cd react-ts-antd-template-electron
  3. yarn install/npm install
  4. yarn start/npm start

然后在 vscode 运行,方便调试/重启等, .vscode 的运行配置也一并上传了

或者在另一个终端 运行 yarn electron-startnpm electron-start,启动 electron 应用

网页版看 这里

ipcRender 在网页上面的简单使用

参考 这里

  • 在根目录创建 electron-src/preload.js
  • package.json 中:

    开发运行的时候没问题,但是不将文件包含进去打包的话,安装包不包含这个文件就会报错,找不到 preload .js 了

    build.files 添加 preload.js 文件,我放在 electron-src 下,所以将这个目录都包含进去

    1. "build": {
    2. "appId": "com.example.electron-cra",
    3. "files": [
    4. "build/**/*",
    5. "node_modules/**/*",
    6. "electron-src/**/*",
    7. "main.js"
    8. ],
    9. "directories": {
    10. "buildResources": "package"
    11. }
    12. },
  • main.js 中修改:
    1. mainWindow = new BrowserWindow({
    2. width: 800,
    3. height: 600,
    4. webPreferences: {
    5. nodeIntegration: true, // 是否集成 Nodejs
    6. webSecurity: false,
    7. preload: __dirname + '/electron-src/preload.js'
    8. }
    9. });
  • preload.js 如下:
    1. window.ipcRenderer = require('electron').ipcRenderer;
  • 在 common.d.ts 中添加
    1. interface Window {
    2. ipcRenderer: any
    3. }
  • 使用的时候:
    1. const ipcRenderer = window.ipcRenderer;
    2. if (ipcRenderer) {
    3. // electron 中打开
    4. } else {
    5. // 网页中打开
    6. }

electron 打包

可以看 electron-app 分支