首先,我使用的是 Vuejs 和 Electron,package.json 将在本文末尾。
我正在尝试使用此侦听器来显示我的窗口,如电子文档推荐的那样。
win.once("ready-to-show", () => win.show());
但似乎这从未触发,只有当我手动获取变量win并调用函数show()时。
下面是我的 background.js(运行 Electron 主进程的文件)文件和我的 windowManager.js 文件(我为构建我的窗口并返回它们而创建的文件,我将此文件导入到 background.js 中)
//background.js
"use strict";
import { app, protocol, BrowserWindow, ipcMain } from "electron";
import installExtension, { VUEJS_DEVTOOLS } from "electron-devtools-installer";
import { windowManager } from "./backend/windowManager.js";
const isDevelopment = process.env.NODE_ENV !== "production";
let winHome, winMain;
// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
{ scheme: "app", privileges: { secure: true, standard: true } },
]);
// Quit when all windows are closed.
app.on("window-all-closed", () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", async () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) {
winHome = await windowManager.createHomeWindow();
winHome.show();
}
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on("ready", async () => {
if (isDevelopment && !process.env.IS_TEST) {
// Install Vue Devtools
try {
await installExtension(VUEJS_DEVTOOLS);
} catch (e) {
console.error("Vue Devtools failed to install:", e.toString());
}
}
winHome = await windowManager.createHomeWindow();
winHome.show();
});
// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
if (process.platform === "win32") {
process.on("message", (data) => {
if (data === "graceful-exit") {
app.quit();
}
});
} else {
process.on("SIGTERM", () => {
app.quit();
});
}
}
// IPC callback listeners
const closeWindow = () => {
BrowserWindow.getFocusedWindow().close();
};
const openMainWindow = async () => {
winMain = await windowManager.createMainWindow();
winHome.close();
};
// Setting the IPC listeners
ipcMain.on("close-window", closeWindow);
ipcMain.on("open-main-window", openMainWindow);
//windowManager.js
import { BrowserWindow } from "electron";
import { createProtocol } from "vue-cli-plugin-electron-builder/lib";
import * as path from "path";
export const windowManager = {
async createWindow(sizeProperties, settings, router = null) {
let win;
// Create the browser window.
win = new BrowserWindow({
...sizeProperties,
...settings,
webPreferences: {
// Use pluginOptions.nodeIntegration, leave this alone
// See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION,
preload: path.join(__dirname, "preload.js"),
},
});
//Checking with was sent some router
router = router ? `/#/${router}` : "";
try {
if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode
await win.loadURL(`${process.env.WEBPACK_DEV_SERVER_URL}${router}`);
if (!process.env.IS_TEST) win.webContents.openDevTools();
} else {
createProtocol("app");
// Load the index.html when not in development
await win.loadURL(`app://./index.html${router}`);
}
} catch (error) {
console.error(error);
}
//Setting the default behavior for window
win.once("ready-to-show", () => {
win.show();
});
win.on("closed", () => {
win.destroy();
});
//TODO Check if this line is necessary
win.show();
return win;
},
async createHomeWindow() {
return await this.createWindow(
{
width: 706,
height: 550,
},
{
frame: false, //Remove frame to hide default menu
resizable: false,
show: false, //Don't show at the begin
}
);
},
async createMainWindow() {
let win = await this.createWindow(
{
width: 800,
height: 550,
},
{
frame: false, //Remove frame to hide default menu
show: false, //Don't show at the begin
},
"main"
);
//Extra window behavior
win.maximize();
return win;
},
};
和 package.json
{
"name": "simulans",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint",
"electron:build": "vue-cli-service electron:build",
"electron:serve": "vue-cli-service electron:serve",
"postinstall": "electron-builder install-app-deps",
"postuninstall": "electron-builder install-app-deps"
},
"main": "background.js",
"dependencies": {
"core-js": "^3.6.5",
"vue": "^2.6.11",
"vue-router": "^3.0.3",
"vuex": "^3.4.0"
},
"devDependencies": {
"@vue/cli-plugin-babel": "~4.5.0",
"@vue/cli-plugin-eslint": "~4.5.0",
"@vue/cli-plugin-vuex": "~4.5.0",
"@vue/cli-service": "^3.0.5",
"@vue/eslint-config-prettier": "^6.0.0",
"babel-eslint": "^10.1.0",
"electron": "^13.0.1",
"electron-devtools-installer": "^3.1.0",
"eslint": "^6.7.2",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-vue": "^6.2.2",
"prettier": "^2.2.1",
"vue-cli-plugin-electron-builder": "~2.0.0",
"vue-template-compiler": "^2.6.11"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:recommended",
"@vue/prettier"
],
"parserOptions": {
"parser": "babel-eslint"
},
"rules": {
"prettier/prettier": 0
}
},
"browserslist": [
"> 1%",
"last 2 versions",
"not dead"
]
}