ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-02-09 09:35:37  阅读:211  来源: 互联网

标签:vue env process app electron ec PC 跨平台 https


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

标签:vue,env,process,app,electron,ec,PC,跨平台,https
来源: https://blog.csdn.net/Listest/article/details/122810810

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有