【最简单】Electron 怎么将网页打包成EXE

【最简单】Electron 怎么将网页打包成EXE,第1张

前期准备:

1、你已经安装并配置好了 node.js (全局安装)

2、你已经用 npm 安装了 electron (全局安装)

3、你已经写好了前端网世族页(html、css、javascript 这些,或者基于这些的前端旁铅框架写好的网页

4、以上三点看不懂的,赶紧去百度。。。

你如果具备了以上的假设,请继续往下看:

1、找到你的前端网页项目文件夹,新建 package.json、main.js、index.html 三个文件(注:其中的 index.html 是你的网页首页)

你的项目目录/

├── package.json

├── main.js

└── index.html

2、在 package.json 中添加如下内容

{

  "name"    : "app-name",

  "version" : "0.1.0",

  "main"    : "main.js"

}

3、在 main.js 中添加下面的内容,这个 main.js 文件就是上面 package.json 中的 "main"键 的值,所以可根据需要修改

const {app, BrowserWindow} = require('electron')

const path = require('path')

const url = require('url')

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

function createWindow () {

  // Create the browser window.

  win = new BrowserWindow({width: 800, height: 600})

  // and load the index.html of the app.

  win.loadURL(url.format({

    pathname: path.join(__dirname, 'index.html'),

    protocol: 'file:',

    slashes: true

  }))

  // Open the DevTools.

  // win.webContents.openDevTools()

  // Emitted when the window is closed.

  win.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.

    win = 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 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', () =>{

  // 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 (win === 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.

4、如果你的网页首页的文件名不是 “index.html”,那么请在 main.js 中将其中的 'index.html' 修改为你的网页首页名

5、打开 DOS,cd 到你的项目目录(或直接在你的项目目录下空白的地方 shift+鼠标右键,然后点击在此处打开命令窗口,这里看不懂的,唉,百度吧少年)

6、在上一步的 DOS 下,输入 npm install electron-packager -g全局安装我们的打包神器

npm install electron-packager -g

7、安装好打包神器后,还是在上一步的 DOS 下,输入 electron-packager . app --win --out presenterTool --arch=x64 --version 1.4.14 --overwrite --ignore=node_modules 即可开始打包

electron-packager . app --win --out presenterTool --arch=x64 --version 1.4.14 --overwrite --ignore=node_modules

这个命令什么意思?蓝色部分可自行修改:

electron-packager . 可执行文件的文件名 --win --out 打包成的文件夹名 --arch=x64位还是32位 --version版本号 --overwrite --ignore=node_modules

如果:

执行打包命令的时候,报以下提示

–version does not take an argument. Perhaps you meant --app-version or --electron-version?

后在贴吧找到解决方法,将–version 改成了–electron-version

electron-packager . app --win --out demoapp --arch=x64 --electron-version 1.4.14 --overwrite --ignore=node_modules

8、打包成功后,会生成一个新的文件夹,点进去,找到 exe 文件,双击就可以看到网页变成了一个桌面应用啦!

以上是最简单的打包方式,至于怎么修改窗口大小、菜单栏怎么加、怎么调用系统API这些,就给你慢慢去研究Electron了。

里面有我已将内容为 hello,world 的 index.html 网页通过 Electron 框架打包为 windows 环境下的桌面应用。

现只需将你的网页前端项目复制到 /resources/app/project 目录下,双击 exe 文件即可以桌面应用的方式运行你的网页。

EXE是

Windows平台的可执行程序,网站则是由超槐肢文本语言HTML、CSS、JavaScript如果网站是动铅岁世态的还会有后台编程(脚本雀缓)语言组成,所以是没办法编译成EXE文件的。

EXE文件是由C、C++、C#、VB等编程语言书写编译生成的,和网站是两码事。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12388942.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存