PC端 -跨平台应用开发-ec

electron+vue项目添加vue-devTools - 简书0.nvm use 16.13.1,可以考虑 使用最新版本的node

1.npm install @vue/cli -g

2.vue create electron-vue-demo 创建vue 项目

3. 

3.cd electron-vue-demo

4.vue add electron-builder

5.修改主进程代码,修改后代码如下:隐藏菜单栏和更改图标

'use strict'

import { app, protocol, BrowserWindow, Menu, globalShortcut, session } from 'electron'
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
const isDevelopment = process.env.NODE_ENV !== 'production'

// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
  { scheme: 'app', privileges: { secure: true, standard: true } }
])

async function createWindow () {
  // Create the browser window.
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      webSecurity: false,
      // Use pluginOptions.nodeIntegration, leave this alone
      // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
      nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
      contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION
    },
    //这里的${__static}对应的是public目录
    icon: `${__static}/favicon.ico` 
  })

  globalShortcut.register('CommandOrControl+F12', function () {
    win.webContents.openDevTools()
  })
  createMenu()

  if (process.env.WEBPACK_DEV_SERVER_URL) {
    // Load the url of the dev server if in development mode
    await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
    if (!process.env.IS_TEST) win.webContents.openDevTools()
  } else {
    createProtocol('app')
    // Load the index.html when not in development
    win.loadURL('app://./index.html')
  }
}

function createMenu() {
     // darwin表示macOS,针对macOS的设置
    if (process.platform === 'darwin') {
        const template = [
            {
                label: 'App Demo',
                submenu: [
                    {
                        role: 'about'
                    },
                    {
                        role: 'quit'
                    }
                ]
            }
        ]
        let menu = Menu.buildFromTemplate(template)
        Menu.setApplicationMenu(menu)
    } else {
        // windows及linux系统
        Menu.setApplicationMenu(null)
    }
}

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  // On macOS it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (BrowserWindow.getAllWindows().length === 0) createWindow()
})

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', async () => {
  if (isDevelopment && !process.env.IS_TEST) {
    // Install Vue Devtools
    // try {
    //   await installExtension(VUEJS_DEVTOOLS)
    // } catch (e) {
    //   console.error('Vue Devtools failed to install:', e.toString())
    // }
     // Install Vue Devtools
     try {
        const path = require("path");
        session.defaultSession.loadExtension(
            path.resolve(__dirname, "./devTools/chrome")  //这个是刚刚build好的插件目录
        );
     } catch (e) {
        console.error("Vue Devtools failed to install:", e.toString());
     }
  
  } 
  
  createWindow()
})

// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
  if (process.platform === 'win32') {
    process.on('message', (data) => {
      if (data === 'graceful-exit') {
        app.quit()
      }
    })
  } else {
    process.on('SIGTERM', () => {
      app.quit()
    })
  }
}

6.打包-这个可能会遇见不同的问题,比较麻烦。npm run electron:build  执行命令后可以参考下面的这个文章。

解决electron打包下载依赖超时,报错等问题 - 简书一、vue项目引入electron-builder 这里相当于前言 使用vue-cli构建electron应用,使用vue add electron-builder,添加打包...https://www.jianshu.com/p/35abb68d0331需要用的文件:

     electron相关资源-Web开发文档类资源-CSDN下载electron相关资源更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/Listest/79687864

electron-builder相关资源-Web开发文档类资源-CSDN下载electron-builder相关资源更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/Listest/79687882

7.参考文章:

手把手教你使用Electron5+vue-cli3开发跨平台桌面应用 - 掘金Electron是一个基于Chromium和 Node.js,可以使用 HTML、CSS和JavaScript构建跨平台应用的技术框架,兼容 Mac、Windows 和 Linux。虽然B/S是目前开发的主流,但是C/S仍然有很大的市场需求。受限于浏览器的沙盒限制,网页应用无法…https://juejin.cn/post/6844903878429769742#heading-348.安装Vue调试神器vue-devtools安装,参考:

electron+vue项目添加vue-devTools - 简书手动clone项目vue-devtoolsgit clone https://github.com/vuejs/vue-devtools.git然后切换到master分支,默...https://www.jianshu.com/p/cc48d4520de3

Vue调试神器vue-devtools安装 - 简书前言vue-devtools是一款基于chrome游览器的插件,用于调试vue应用,这可以极大地提高我们的调试效率。接下来我们就介绍一下vue-devtools的安装。 ch...https://www.jianshu.com/p/63f09651724c该扩展程序未列在 Chrome 网上应用店中,并可能是在您不知情的情况下添加的解决办法 - 知乎1.将下载的文件解压,得到.crx和其它几个文件 2.将该文件后缀改成rar 3.然后再次解压这个rar文件 就会得到很多个js、html等文件,其实你已经成功了,这样; 其实本来crx是一个文件添加不能用,但是转成rar在解压就…https://zhuanlan.zhihu.com/p/106343392

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Listest

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值