electron开启node api支持

1,508 阅读1分钟
const createWindow = () => {
    // Create the browser window.
    const mainWindow = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
            nodeIntegration: true,      //教程只有此项,无下方两个参数设置,如需支持node内方法需设置此三项
            contextIsolation: false,
            enableRemoteModule: true
        }
    });

    // and load the index.html of the app.
    mainWindow.loadFile(path.join(__dirname, 'index.html')).then(r => {
        console.log(r)
    });

    // Open the DevTools.
    mainWindow.webContents.openDevTools();
};

在看教程中发现只有nodeIntegration参数开启,electron12以上都需要配置contextIsolation and enableRemoteModule