点击应用程序菜单项Electron时出错

lxkprmvk  于 6个月前  发布在  Electron
关注(0)|答案(2)|浏览(61)

当我点击一个菜单项时,我得到一个错误:


的数据
我该怎么解决这个问题?
代码:
Main.js

const { Menu } = require('electron')
const { createNewFile } = require('./js/createNewFile.js')

const menu = [
{
    label: 'File',
    submenu: [
    {
        label: 'New',
        click: () => {
            createNewFile()
        },
    }
}];

字符串
createNewFile.js

function createNewFile() {
    document.getElementById('newFileWindow').classList.remove('hiddenFileWindow')
}
module.exports = createNewFile


当使用console.log(...)时,函数也不起作用。
可能是因为我有一个非只读的node_modules文件夹吗?

kfgdxczn

kfgdxczn1#

您缺少了一个步骤,主进程无法访问渲染器进程的DOM。如果您想从菜单中更改DOM中的某些内容,则需要使用IPCpreload文件在进程之间进行通信。例如:

renderer.js

window.electronAPI.createNewFile(() => {
  document.getElementById('newFileWindow').classList.remove('hiddenFileWindow');
});

字符串

preload.js

const { contextBridge, ipcRenderer } = require('electron');

contextBridge.exposeInMainWorld('electronAPI', {
  createNewFile: (callback) => ipcRenderer.on('newFileWindow', callback)
});

main.js

const menu = [{
  label: 'File',
  submenu: [{
    label: 'New',
    // `mainWindow` is the window you created with `new BrowserWindow()`
    click: () => mainWindow.webContents.send('createNewFile')
  }]
}];

u59ebvdq

u59ebvdq2#

您是否在webPreferences中阻止了安全防范措施?试试这个:

webPreferences: {
  sandbox: false,
  nodeIntegration:true,
  contextIsolation: false,
  webSecurity: true,
}

字符串

相关问题