javascript - 尽管关注了有关主题的所有堆栈溢出文章,但无法将 Electron 初始化为 Angular 项目,反之亦然
问题描述
所以我试图将一个电子项目初始化为一个角度项目。当我运行ng build --prod && electron .
orng build --base-href ./ && electron .
时,我会收到两个错误之一,具体取决于我放置电子 main.js 文件的位置。在 src 文件中,我收到错误:
Uncaught Exception:
/Users/jaredjackson/Programming/Git/cinema-center/src/main.js:1
import { app, BrowserWindow } from 'electron';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at wrapSafe
这是我的 package.json 文件:
{
"name": "cinema-center",
"version": "0.0.0",
"main": "src/main",
"scripts": {
"ng": "ng",
"start": "ng build --prod && electron .",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"electron": "electron ."
当我将 main.js 文件放在根项目目录中并更新 package.json 时,我收到此错误:
Unable to find Electron app at /Users/jaredjackson/Programming/Git/cinema-center. Cannot find module '/Users/jaredjackson/Programming/Git/cinema-center/main.js'. Please verify that the package.json has a valid "main" entry
我也添加"type": "module",
到包 .json 中,它不起作用。
我使用ng new
andnpm install --save-dev electron
以及npm install --save-dev -g electron
. 我还尝试通过首先从 electron-forge 安装电子然后运行来重做它ng new
。
这是我的 main.js 文件:
import { app, BrowserWindow } from 'electron';
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
app.quit();
}
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
const createWindow = () => {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 800,
height: 600,
});
// and load the index.html of the app.
mainWindow.loadURL(`file://${__dirname}/index.html`);
// Open the DevTools.
mainWindow.webContents.openDevTools();
// Emitted when the window is closed.
mainWindow.on('closed', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
};
// 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', createWindow);
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On OS X 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', () => {
// On OS X 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 (mainWindow === null) {
createWindow();
}
});
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.
我的节点版本是 v14.16.1,我的 npm 版本是 7.14.0。Angular Cli 是 11.2.14。
解决方案
electron 使用 CommonJS 模块,将 main.js 头文件更改为以下内容:
const { app, BrowserWindow } = require("electron");
const url = require("url");
const path = require("path");
推荐阅读
- python - 对于 as 表达式
- sql - 如何在具有多个条件的 SQL 中透视数据
- php - Codeigniter 循环使用单个数据更新记录
- python - /cart/chelsea-01920-jersey/ int() 以 10 为基数的无效文字:'color'
- powershell - Powershell 将心跳更新为 cloudwatch
- sql-server - 一次仅在一个子记录中将位字段限制为真
- c++ - C++ 表达式 SFINAE 和 ostream 操纵器
- c# - 新类对象错误:对象引用未设置为对象的实例
- node.js - 模块负载平衡的轮询不起作用
- ios - 编辑 MessageKit 布局