Electron-02-创建并运行helloworld项目

新建工作区

  1. 在F盘新建F:\Electron_WorkSpace用来存放项目目录

建立helloworld项目

  1. 建立F:\Electron_WorkSpace\helloworld目录并进入。
  2. npm init初始化项目:配置文件一路回车,我们暂不做配置,会新建一个package.json
    在这里插入图片描述
  3. npm install electron --save-dev安装electron作为helloworld项目的依赖。此命令执行时间有点长,因为要安装很多的依赖文件。
    在这里插入图片描述
    可以看到目录下多了很多文件
    在这里插入图片描述

编写代码

我们要关注的文件有三个

  1. package.json 通过npm init自动生成,需要修改
  2. index.js 自己创建一个主进程文件,需要些代码
  3. index.html 主窗口显示的页面,需要写代码
    在这里插入图片描述

package.json修改"script"

{
  "name": "helloworld",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
   "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "electron": "^5.0.6"
  }
}

修改为

{
  "name": "helloworld",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "electron": "^5.0.6"
  },
  "devDependencies": {},
  "scripts": {
    "start": "electron ."
  },
  "author": "",
  "license": "ISC"
}


index.js

const electron = require("electron");
const url = require("url");
const path = require("path");

const {app, BrowserWindow} = electron;

let mainWindow;
//等待app准备好
app.on('ready', function() {
    //创建新窗口
    mainWindow = new BrowserWindow({});
    //将html加载到窗口
    mainWindow.loadURL(url.format({
        pathname: path.join(__dirname, "index.html"),
        protocol: "file",
        slashes: true
    }));

    //当关闭窗口是退出app
    mainWindow.on("closed", function() {
        app.quit();
    });
});


index.html

<html>
    <head>
        <title>HelloWorld</title>
    </head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>

运行

npm start

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值