Interprocess communication ipcRenderer, ipcMain website is very clear

But the problem is that I used vue-cli-plugin-electric-Builder to introduce electron

Want to do interprocess communication

import {ipcRenderer} from ‘electron’

Import keeps reporting errors

Here’s why

The plugin documentation

Method 1: Node Integration

# vue.config.js
module.exports = {
  pluginOptions: {
    electronBuilder: {
      nodeIntegration: true
    }
  }
}
Copy the code

Method 2: Preload

1. Create a js file under SRC

# ./src/preload.js

import { ipcRenderer } from 'electron'
window.ipcRenderer = ipcRenderer
Copy the code

2. Modify the vue. Config. Js

# ./vue.config.js
module.exports = {
  pluginOptions: {
    electronBuilder: {
      preload: 'src/preload.js',
    }
  }
}
Copy the code

3. Modify the main process file

# ./src/background.js const win = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: Process.env.electron_node_integration, // Notice the new line + preload: path.join(__dirname, 'preload.js')}})Copy the code

Use 4.

Window. IpcRenderer canCopy the code