添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am using Vue CLI 3 and vue-cli-plugin-electron-builder to package my Vue Electron app and I am not able to get my preload.js script for electron working.

main window

win = new BrowserWindow({
  width: 800,
  height: 600
  webPreferences: {
    nodeIntegration: false,
    preload: path.join(__dirname, "/../src/preload.js") // works but window.electron.dialog in undefined

preload.js

const { dialog } = require("electron");
window.electron = {};
window.electron.dialog = dialog;

The window.electron.dialog is always undefined in my Vue component - the import is clearly not working. Note that window.electron is defined properly. I must be missing something.

Add the following lines into the file vue.config.js , if the file does not exist create one in the root folder of your project

module.exports = {
 //...
 pluginOptions: {
  electronBuilder: {
    preload: 'src/preload.js',
    // Or, for multiple preload files:
    // preload: { preload: 'src/preload.js', otherPreload: 'src/preload2.js' }
 //...

Check the documentation for more #preload-files

The solution was more simple than expected. Imports work in window.onload event.

window.onload = () => {
  const { dialog } = require("electron").remote;
  window.electron = {};
  window.electron.dialog = dialog; // now set properly
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.