Angular Electron在第一次加载时不工作



我曾尝试用Electron构建一个Angular应用程序,但我遇到了以下问题:当应用程序启动时,浏览器窗口为空,控制台中没有任何内容。全身标记也是空的。然而,在我在Electron浏览器中手动按下重载后,它就工作了——一切都按预期运行。你知道为什么会发生这种事吗?

这是我的main.js文件:

const {app, BrowserWindow} = require('electron');
const {autoUpdater} = require("electron-updater");
const log = require('electron-log');
const url = require("url");
const path = require("path");
var webpackTargetElectronRenderer = require('webpack-target-electron-renderer');

autoUpdater.logger = log;
autoUpdater.logger.transports.file.level = 'info';
log.info('App starting...');
let win;
function sendStatusToWindow(text) {
log.info(text);
console.log(text);
mainWindow.console.log(text);
win.webContents.send('message', text);
}
// function createDefaultWindow() {
//   win = new BrowserWindow();
//   win.webContents.openDevTools();
//   win.on('closed', () => {
//     win = null;
//   });
//   win.loadURL(`file://${__dirname}/version.html#v${app.getVersion()}`);
//   return win;
// }
app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
// On certificate error we disable default behaviour (stop loading the page)
// and we then say "it is all fine - true" to the callback
console.log(JSON.stringify(certificate));
event.preventDefault();
callback(true);
});
let mainWindow

var options = {
// webPreferences: {
//   webSecurity: false
// },
node: {
__dirname: false
}
}
options.target = webpackTargetElectronRenderer(options)
function createWindow () {
mainWindow = new BrowserWindow({
width: 1000,
height: 800,
webPreferences: {
nodeIntegration: true
}
// webPreferences: {
// plugins: true,
// nodeIntegration: true,
// webSecurity: false
// }
})
// log.info('Hello, log');
// log.info(log.transports.console);
// console.log(log.transports.console);
// log.warn('Some problem appears');
// // log.info("Test");
// console.log = log.info;
console.log("TEst");
// log.info(log.transports.file.getFile());
mainWindow.loadURL(
url.format({
pathname: path.join(__dirname, `/dist/index.html`),
protocol: "file:", 
slashes: true
})
);
// mainWindow.webContents.devToolsWebContents("Test");
// var x = path.join(__dirname, `/dist/index.html`);
// console.log(x);
// mainWindow.loadURL(`file://${__dirname}/version.html#v${app.getVersion()}`);
// Open the DevTools.
mainWindow.webContents.openDevTools();
// console.log("Test");
mainWindow.on('closed', function () {
mainWindow = null
})
autoUpdater.on('checking-for-update', () => {
sendStatusToWindow('Checking for update...');
})
autoUpdater.on('update-available', (ev, info) => {
sendStatusToWindow('Update available.');
})
autoUpdater.on('update-not-available', (ev, info) => {
sendStatusToWindow('Update not available.');
})
autoUpdater.on('error', (ev, err) => {
sendStatusToWindow('Error in auto-updater.');
})
autoUpdater.on('download-progress', (ev, progressObj) => {
sendStatusToWindow('Download progress...');
})
autoUpdater.on('update-downloaded', (ev, info) => {
sendStatusToWindow('Update downloaded; will install in 5 seconds');
});
}
// app.on('ready', createWindow)
app.on('ready', function()  {
createWindow();
autoUpdater.checkForUpdatesAndNotify();
});
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
app.on('activate', function () {
if (mainWindow === null) createWindow()
})

找到了一个临时答案:在加载后手动重新加载窗口。我会设法想出更好的办法。

最新更新