Node.js | Electron开发一个简易的 Electron

第一次使用Electron,记录一下第一个跑起来的应用

一、首先安装Node.js长期支持版

https://nodejs.org/zh-cn/

二、安装Electron(全局)

npm install electron -g

三、新建一个空文件夹(my-app)

在文件夹下打开命令行输入

npm init

此时文件夹下多了一个 package.json 的文件

{
  "name": "my-app",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "author": "",
  "license": "ISC"
}

四、加载依赖,命令行输入

npm install

五、新建 main.js

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

function createWindow() {
  // 创建浏览器窗口
  let win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  })

  // 加载index.html文件
  win.loadFile('index.html')
}

app.whenReady().then(createWindow)

六、新建 index.html(你自己需要展示的页面)

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
    <!-- https://electronjs.org/docs/tutorial/security#csp-meta-tag -->
    <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
  </head>
  <body>
    <h1>Hello World!</h1>
    We are using node <script>document.write(process.versions.node)</script>,
    Chrome <script>document.write(process.versions.chrome)</script>,
    and Electron <script>document.write(process.versions.electron)</script>.
  </body>
</html>

七、启动,在命令行输入

npm start

你可能感兴趣的:(Node.js)