1. npm init
    2. ...
    3. entry file(index.js): main.js

    安装开发依赖

    1. npm i -D electron

    image.png
    添加启动脚本
    image.png
    添加 main.js 入口文件
    image.png
    使用 npm start 启动应用,将在控制台输出 Hello World。

    修改 main.js:

    1. const {app, BrowserWindow} = require('electron')
    2. function createMainWindow(){
    3. const mainWindow = new BrowserWindow({
    4. title:'Image Shink',
    5. width: 500,
    6. height:600
    7. })
    8. }
    9. app.on('ready',createMainWindow)

    preload.js

    1. // 1. import electron objects
    2. const { app, BrowserWindow } = require("electron");
    3. const path=require('path');
    4. // 2. reserve a reference to window object
    5. let window;
    6. // 3. wait till application started
    7. app.on("ready", () => {
    8. // 4. create a new window
    9. window = new BrowserWindow({
    10. width: 800,
    11. height: 600,
    12. webPreferences: {
    13. nodeIntegration: true,
    14. webSecurity: false, // 允许跨域
    15. preload: path.join(__dirname, 'preload.js'),
    16. },
    17. });
    18. // 5. load window content
    19. // window.loadFile("index.html");
    20. window.loadURL("http://www.baidu.com");
    21. });
    1. // window.$ = require('jquery')
    2. window.onload = function(){
    3. const $ = require('jquery')
    4. console.log("preload loaded")
    5. // document.getElementById("s_map").style("display","none")
    6. console.log("set by id")
    7. $("#kw").css({"background":"red"})
    8. console.log("set by jquery")
    9. }