【发布时间】:2019-11-14 18:52:27
【问题描述】:
所以我正在尝试使用 angular(Typescript 版本)制作一个电子应用程序,它根本不会启动,我已经集成了不做任何事情的 Electron-log(这就是我认为它根本不启动的原因)。
const path = require('path');
const url = require('url');
const log = require('electron-log');
log.transports.file.level = 'debug';
log.transports.file.format = '{h}:{i}:{s}:{ms} {text}';
log.transports.file.file = __dirname + './log.txt';
log.transports.file.streamConfig = { flags: 'w'};
log.info('message');
// Keep a global reference of the window object, if we don't the window will be closed
// automagically when the JS object is Garbage Collected. (GC from here on out)
let win;
try {
const createWindow = () => {
// set timeout to render the window not until the Angular compiler is ready to show the project
//create the browser window
win = new BrowserWindow({
width: 800,
height: 600,
icon: path.join(__dirname, 'favicon.ico'),
});
//and load the app
win.loadURL(url.format({
pathname: path.join(__dirname, 'resources/app/angular-flask/index.html'),
protocol: 'file:',
slashes: true
}));
// Emitted when the window is closed
win.on('closed', () => {
// Dereference the window object
win = null;
});
};
// This method will be called when Electron has finished initialization and is ready to
// create browser windows . Some API's can only be used after this event happens.
app.on('ready', createWindow);
// Quit when all windows are closed
app.on('windows-all-closed', () => {
// On macOS applications might stay active untill user quits with cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// On macOS sometimes a window is recreated when the dock icon is clicked and there are no other
// windows open
if (win === null) {
createWindow();
}
});
} catch(e) {
log.error(e)
}
以上是我的 Electron Production 代码,也可以在 pastebin 上找到,谁能解释为什么会发生这种情况并提供解决方案?
编辑:在我集成电子日志之前它不起作用。当我通过 npx lite-server 启动它时,角度前端也可以工作。
EDIT2:这是我的 Package.json 和我更新的 main.js 代码 electron.prod.js 我也在使用电子打包器。
【问题讨论】:
-
在您集成“电子日志”之前它是否有效?
-
不,在整合 @Kyle 之前我没有工作
-
我们需要更多。 package.json 至少会有所帮助。在 BrowserWindow 中以
webPreferences: { devTools: true }开头,在应用程序准备就绪时以mainWindow.webContents.openDevTools();开头。检查控制台是否存在您正在加载的资产 (index.html) 的问题。我知道这很令人沮丧。我刚刚在 Mac 上遇到了类似的问题,它可以工作,但在与 electron-builder 签署后无法加载资产。 -
@narmageddon 我添加了您所说的内容,但是当我单击 exe 时,甚至没有弹出窗口。我还用 package.json 和更新的代码更新了问题
-
@Deathshiver 好的,我可以在
resources/app/angular-flask/index.html找到索引页面。 BrowserWindow 也没有定义。我必须在文件顶部添加 const { app, BrowserWindow } = require("electron");`。我还将 loadUrl 更改为:win.loadURL('file://${path.join(__dirname,"../resources/app/angular-flask/index.html")}');。我从来没有像你那样做过url.format,但我知道我在 Mac 和 Windows 上的工作方式。
标签: javascript angular typescript electron