如何将现有的角度应用程序移植到电子? [英] How to port an existing angular app to electron?

查看:21
本文介绍了如何将现有的角度应用程序移植到电子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是角度的初学者和电子的完全新手.我想知道是否可以通过使用 webapp 的大部分现有代码库将现有的 angular 应用程序移植到电子?

I am a beginner to angular and a complete noob to electron. I was wondering if it would be possible to port an existing angular app to electron by using most of the existing codebase of the webapp?

我在这方面找到了几个链接,但在 Google 上发现的不多.大多数结果都谈到从 angular 和 electron 开始创建桌面应用程序 - 这是我的意图,但我想使用我现有的大部分代码库,理想情况下只是添加 webpack 和电子相关配置来从现有代码库编译电子应用程序,因为我不想维护同一代码库的两个版本.

I found a couple of links on this but not much on Google. Most results talk about starting with angular and electron to create a desktop app - which is my intention but I would like to use most of my existing code base and ideally just add webpack and electron related config to compile the electon app from existing codebase as I don't want to maintain two version of the same codebase.

我可以使用这个桥接器来访问 Electron API(虽然我不确定它们是如何连接在一起的)https://github.com/develephant/ngElectron

I could use this bridge to access Electron API (although I am not sure how it will all hang together) https://github.com/develephant/ngElectron

人们通常会做什么?

推荐答案

以下是我将 Electron 添加到现有 AngularJS 应用程序的步骤.
1. 克隆现有的 Angular 应用 git clone https://github.com/angular/angular-seed.git.
2. cd angular-seed.
3. npm install --save electron.
4. 在/app中,创建一个main.js文件并粘贴:

Here are the steps I took to add Electron to an existing AngularJS app.
1. Clone an existing Angular app git clone https://github.com/angular/angular-seed.git.
2. cd angular-seed.
3. npm install --save electron.
4. In /app, create a main.js file and paste:

const electron = require('electron')
// Module to control application life.
const app = electron.app
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow

// 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

function 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', function () {
    // 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', function () {
  // 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', function () {
  // 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 require them here.

  1. 在/app 中,创建一个空白的 renderer.js 文件.
  2. 在 package.json 的根目录中添加 "main": "main.js".
  3. 在 package.json 的 "scripts" 部分将 "start" 更改为 "start": "electron .".
  4. npm install &&npm start.

这篇关于如何将现有的角度应用程序移植到电子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆