• electron初探


    一.electron框架一般很难下载,使用淘宝镜像

    npm install -g cnpm --registry=https://registry.npm.taobao.org
    

    二.示例工程目录

    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>
    

    main.js

    const { app, BrowserWindow } = require("electron");
    
    function createWindow() {
      // 创建浏览器窗口
      const win = new BrowserWindow({
         800,
        height: 600,
        webPreferences: {
          nodeIntegration: true,
        },
      });
    
      // 并且为你的应用加载index.html
      win.loadFile("index.html");
    }
    
    // Electron会在初始化完成并且准备好创建浏览器窗口时调用这个方法
    // 部分 API 在 ready 事件触发后才能使用。
    app.whenReady().then(createWindow);
    
    //当所有窗口都被关闭后退出
    app.on("window-all-closed", () => {
      // 在 macOS 上,除非用户用 Cmd + Q 确定地退出,
      // 否则绝大部分应用及其菜单栏会保持激活。
      if (process.platform !== "darwin") {
        app.quit();
      }
    });
    
    app.on("activate", () => {
      // 在macOS上,当单击dock图标并且没有其他窗口打开时,
      // 通常在应用程序中重新创建一个窗口。
      if (BrowserWindow.getAllWindows().length === 0) {
        createWindow();
      }
    });
    

    初始化项目 yarn init

    package.json

    {
      "name": "my-electron",
      "version": "1.0.0",
      "description": "测试项目",
      "main": "main.js",
      "scripts": {
        "start": "electron ."
      },
      "author": "shimon",
      "license": "MIT"
    }

    安装依赖

    cnpm install --save-dev electron

    启动项目

    yarn start

  • 相关阅读:
    【转】嵌入式程序员应该知道的16个问题
    GetMemory()函数
    Some good questions
    [转]永远做一个有计划的人
    内存分配管理
    c语言面试题(感觉比较好的题目)
    const char*, char const*, char*const的区别
    《论语》《中庸》《大学》经典语录
    洗脑
    python基础之函数参数,名称空间,以及函数嵌套
  • 原文地址:https://www.cnblogs.com/shi2310/p/15893134.html
Copyright © 2020-2023  润新知