简体   繁体   中英

Can't close window in electron

I have this code in my renderer.js:

const ipcRenderer = require('electron').ipcRenderer;
const remote = require('electron').remote;

function sendForm(event) {
    event.preventDefault() // stop the form from submitting
    let code = document.getElementById("code").value;
    ipcRenderer.send('2FacLogin', {twoFactorCode: code});

    // TODO Close Window
    var window = remote.getCurrentWindow();
    window.close();
}

this is how the window is opened:

 const win = new BrowserWindow({
    width: 300,
    height: 300,
    webPreferences: {
        nodeIntegration: true,
        contextIsolation: false,
    }
  })

Why does the window not close?

Since Electron 10.0.0 , the remote module is disabled by default, it must be explicitly enabled when creating a new browser window by adding enableRemoteModule: true to the webPreferences object.

const win = new BrowserWindow({
    width: 300,
    height: 300,
    webPreferences: {
        nodeIntegration: true,
        contextIsolation: false,
        enableRemoteModule: true
    }
})

See: Default Changed: enableRemoteModule defaults to false

BTW, the following statement:

var window = remote.getCurrentWindow();

most certainly triggers an error like:

Uncaught TypeError: Cannot read property 'getCurrentWindow' of undefined

that you can catch yourself by checking the DevTools' console...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM