Electron-Vue入门Demo

前提条件,Electron框架能够正常的运行(electron环境的配置不是本文要说明的部分)
1,安装vue的环境模块

npm install -g vue-cli

2,初始化vue工程

vue init webpack demo

下面几个选项注意
Install vue-router? yes
Use ESLint to lint your code? no
Set up unit tests no
Setup e2e tests with Nightwatch? no

3,修改config/index.js的参数

port:8080
autoOpenBrowser:true
assetsPublicPath: './'

4,运行编译工程

npm run dev
npm run build

5,编译完成后多出一个dist文件夹,后面的electron需要的文件main.js和package.json都在dist中追加
main.js

const {app, BrowserWindow} =require('electron');//引入electron
let win;
let windowConfig = {
  width:800,
  height:600
};//窗口配置程序运行窗口的大小
function createWindow(){
  win = new BrowserWindow(windowConfig);//创建一个窗口
  win.loadURL(`file://${__dirname}/index.html`);//在窗口内要展示的内容index.html 就是打包生成的index.html
  win.webContents.openDevTools();  //开启调试工具
  win.on('close',() => {
    //回收BrowserWindow对象
    win = null;
  });
  win.on('resize',() => {
    win.reload();
  })
}
app.on('ready',createWindow);
app.on('window-all-closed',() => {
  app.quit();
});
app.on('activate',() => {
  if(win == null){
    createWindow();
  }
});

package.json

{
  "name": "demo",
  "productName": "项目名称",
  "author": "作者",
  "version": "1.0.4",
  "main": "main.js",
  "description": "项目描述",
  "scripts": {
    "pack": "electron-builder --dir",
    "dist": "electron-builder",
    "postinstall": "electron-builder install-app-deps"
  },
  "build": {
    "electronVersion": "1.8.4",
    "win": {
      "requestedExecutionLevel": "highestAvailable",
      "target": [
        {
          "target": "nsis",
          "arch": [
            "x64"
          ]
        }
      ]
    },
    "appId": "demo",
    "artifactName": "demo-${version}-${arch}.${ext}",
    "nsis": {
      "artifactName": "demo-${version}-${arch}.${ext}"
    },
    "extraResources": [
      {
        "from": "./static/xxxx/",
        "to": "app-server",
        "filter": [
          "**/*"
        ]
      }
    ],
    "publish": [
      {
        "provider": "generic",
        "url": "http://xxxxx/download/"
      }
    ]
  },
  "dependencies": {
    "core-js": "^2.4.1",
    "electron-packager": "^12.1.0",
    "electron-updater": "^2.22.1"
  }
}

6,运行electron工程,进入到dist目录中,执行以下命令

electron .

OK,大功告成!

猜你喜欢

转载自blog.csdn.net/yhj_911/article/details/84293775