最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue3使用Electron打包成exe的方法與打包報(bào)錯(cuò)解決

 更新時(shí)間:2024年06月26日 09:39:41   作者:夢(mèng)翼橫空  
在前端開(kāi)發(fā)中,Electron是一種常用的工具,它允許開(kāi)發(fā)者使用Web技術(shù)構(gòu)建桌面應(yīng)用程序,本文主要介紹了vue3使用Electron打包成exe的方法與打包報(bào)錯(cuò)解決,具有一定的參考價(jià)值,感興趣的可以了解一下

一、安裝

1.安裝electron

npm install electron --save-dev
npm install electron-builder --save-dev

2.在vue項(xiàng)目根目錄新建文件index.js

// main.js

// Modules to control application life and create native browser window
const { app, BrowserWindow,Menu } = require('electron')

const createWindow = () => {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true, // 啟用 Node.js 集成
      contextIsolation: false, // 禁用上下文隔離(Node.js 集成的一個(gè)選項(xiàng))
      webSecurity: false, // 禁用同源策略
      contextIsolation: false,
      session: {
        cookies: true // 這行確保啟用了cookie
      }

    },
    icon: 'build/.icon-ico/icon.ico'//這里是自動(dòng)生成的圖標(biāo),默認(rèn)情況下不需要改

  })

  // and load the index.html of the app.
  mainWindow.loadFile('./dist/index.html')//如果要本地化,這樣寫(xiě),如果寫(xiě)遠(yuǎn)程的,那這里就是請(qǐng)求的域名
  //隱藏頂部菜單
  // mainWindow.setMenu(null);
  // Open the DevTools.
  // Open the DevTools.
  //mainWindow.webContents.openDevTools()
  mainWindow.maximize();//默認(rèn)最大化
}
//設(shè)置中文菜單,默認(rèn)是英文的
const createMenu = () => {
  const template = [
    {
      label: '文件',
      submenu: [
        {
          label: '退出',
          accelerator: 'CmdOrCtrl+Q',
          click: () => {
            app.quit();
          }
        }
      ]
    },
    {
      label: '查看',
      submenu: [
        { label: '重新加載', accelerator: 'F5', role: 'reload' },
        { label: '全屏', accelerator: 'F11', role: 'togglefullscreen' },
        { label: '開(kāi)發(fā)者工具', role: 'toggledevtools' }
      ]
    }
  ];

  const menu = Menu.buildFromTemplate(template);
  Menu.setApplicationMenu(menu);
}

// 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.whenReady().then(() => {
  createWindow()
  createMenu()//菜單設(shè)置
  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()
  })
})

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

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

3.package.json文件編輯

"scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint",
    "electron:build": "vue-cli-service build && electron-builder",
    "electron:serve": "electron ."
  },
  "build": {
    "productName": "這里是你的項(xiàng)目名",
    "appId": "com.example.這里是appId",
    "win": {
      "icon": "favicon.ico",
      "target": ["nsis", "appx"]
    },
    "directories": {
      "output": "build"
    },
    "files": [
      "dist/**/*",
      "index.js"http://這里是剛才建的index.js
    ]
  },

4.測(cè)試

npm run electron:serve

5.打包

npm run electron:build

二、報(bào)錯(cuò)解決

 解決:打開(kāi)cmd 執(zhí)行 npm config edit

npm config edit

 打開(kāi)配置文件 粘貼以下內(nèi)容

registry=https://registry.npm.taobao.org/
sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
phantomjs_cdnurl=http://npm.taobao.org/mirrors/phantomjs
electron_mirror=https://npm.taobao.org/mirrors/electron/
ELECTRON_MIRROR=http://npm.taobao.org/mirrors/electron/

三、使用

真正用的時(shí)候會(huì)出現(xiàn)各種,比例啟動(dòng)exe的圖標(biāo),cookie。

還有就是,平時(shí)我的打包項(xiàng)目不需要指定域名,如果這里用本地化就需要指定域名。

默認(rèn)配置是不開(kāi)啟cookie的,還有就是用cookie不太好,每次啟動(dòng)exe都需要登錄太麻煩了,推薦使用localStorage永久保存。

我的cookie示例,打包exe時(shí)使用localStorage,其他情況下使用cookie:

 setCookie(cname, cvalue)
 {
    if(process.env.NODE_ENV === "host"){
        localStorage.setItem(cname, cvalue);

    }else{
        document.cookie = cname + "=" + cvalue
    }
    //  var d = new Date();
    //  d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    //  var expires = "expires=" + d.toUTCString();
     //document.cookie = cname + "=" + cvalue

 },
 getCookie(cname) {
    if(process.env.NODE_ENV === "host"){
        return localStorage.getItem(cname);
    }else{
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1);
            if (c.indexOf(name) != -1) {
                return c.substring(name.length, c.length);
            }
        }
        return "";

    }
 },
 clearCookie(cname) {
    if(process.env.NODE_ENV === "host"){
        localStorage.removeItem(cname);
    }else{
        var d = new Date();
        d.setTime(-1);
        var expires = "expires=" + d.toUTCString();
        document.cookie = cname + "=''; " + expires;
    }
},

其他代碼一并粘出來(lái)分享一下。

main.js

const env = process.env.NODE_ENV || 'development';

if(env === "host"){//如果是打包exe需要指定接口域名
    axios.defaults.baseURL = process.env.VUE_APP_INTERFACE
    axios.defaults.withCredentials = true;//跨域請(qǐng)求的全局憑證

}

package.json

"scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint",
    "electron:build": "vue-cli-service build --mode host && electron-builder",//使用.env.host配置打包exe
    "electron:serve": "electron ."
  },
  "build": {
    "productName": "newsaas",
    "appId": "com.example.newsaas",
    "win": {
      "icon": "./icon256.png",//這里是啟動(dòng)圖標(biāo)必須是256*256的png圖
      "target": [
        "nsis",
        "appx"
      ]
    },
    "mac": {
      "target": "dmg"
    },
    "nsis": {
      "oneClick": true,
      "allowToChangeInstallationDirectory": true,
      "perMachine": true,
      "allowElevation": true,
      "createDesktopShortcut": true,
      "createStartMenuShortcut": true,
      "shortcutName": "index"

    },
    "directories": {
      "output": "build"
    },
    "files": [
      "dist/**/*",
      "index.js",
      "public/favicon.ico"
    ]
  },

.dev

NODE_ENV=""
VUE_APP_INTERFACE="這里是我的接口域名,只能本地開(kāi)發(fā)時(shí),做代理用,打包不影響"

.dev.host

NODE_ENV="host"
VUE_APP_INTERFACE="http://這里是我的接口域名"

vue.config.js

const { defineConfig } = require('@vue/cli-service')

module.exports = defineConfig({
    devServer:{
      port:8080,
      allowedHosts: "all",
      webSocketServer:false,
      proxy:{
        "/":{
          target: process.env.VUE_APP_INTERFACE,
          changeOrigin:true,
          pathRewrite:{
            "^/":"/"
          }
        }
      }
    },

    transpileDependencies: true,
    publicPath: './',
    // 輸出文件目錄
    assetsDir: 'static',
    outputDir: process.env.outputDir,
    // eslint-loader 是否在保存的時(shí)候檢查
    lintOnSave: true,
    
    // 生產(chǎn)環(huán)境是否生成 sourceMap 文件
    productionSourceMap: false
})

以上就是全部代碼,如果安裝不了electron,或是安裝后運(yùn)行不起來(lái),改一下npm鏡像源試試,package-lock.json這個(gè)文件記得刪除。

到此這篇關(guān)于vue3使用Electron打包成exe的方法與打包報(bào)錯(cuò)解決的文章就介紹到這了,更多相關(guān)Electron打包成exe與報(bào)錯(cuò)解決內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Element布局組件el-row和el-col的使用

    Element布局組件el-row和el-col的使用

    這篇文章主要介紹了Element布局組件el-row和el-col的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Vue仿支付寶支付功能

    Vue仿支付寶支付功能

    這篇文章主要介紹了Vue仿支付寶支付功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-05-05
  • vue?路由判斷方式

    vue?路由判斷方式

    這篇文章主要介紹了vue?路由判斷方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Vue SPA單頁(yè)面的應(yīng)用和對(duì)比

    Vue SPA單頁(yè)面的應(yīng)用和對(duì)比

    單頁(yè)面是指整個(gè)應(yīng)用程序只有一個(gè)唯一完整的 HTML 頁(yè)面,而其它所謂的頁(yè)面,其實(shí)都是組件片段而已,切換頁(yè)面也只是切換一個(gè) HTML 中顯示不同的組件片段。在今后所有的開(kāi)發(fā)項(xiàng)目都是單頁(yè)面應(yīng)用
    2022-08-08
  • 淺談Vue使用Elementui修改默認(rèn)的最快方法

    淺談Vue使用Elementui修改默認(rèn)的最快方法

    這篇文章主要介紹了淺談Vue使用Elementui修改默認(rèn)的最快方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • element-ui?form表單的動(dòng)態(tài)rules校驗(yàn)功能實(shí)現(xiàn)

    element-ui?form表單的動(dòng)態(tài)rules校驗(yàn)功能實(shí)現(xiàn)

    在vue項(xiàng)目中,有時(shí)候可能會(huì)用到element-ui?form表單的動(dòng)態(tài)rules校驗(yàn),這篇文章主要介紹了element-ui form表單的動(dòng)態(tài)rules校驗(yàn),我們可以巧妙的運(yùn)用element-ui form表單里面form-item的校驗(yàn)規(guī)則來(lái)處理,本文結(jié)合實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07
  • axios發(fā)送post請(qǐng)求,提交圖片類(lèi)型表單數(shù)據(jù)方法

    axios發(fā)送post請(qǐng)求,提交圖片類(lèi)型表單數(shù)據(jù)方法

    下面小編就為大家分享一篇axios發(fā)送post請(qǐng)求,提交圖片類(lèi)型表單數(shù)據(jù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • Vuejs如何通過(guò)Axios請(qǐng)求數(shù)據(jù)

    Vuejs如何通過(guò)Axios請(qǐng)求數(shù)據(jù)

    這篇文章主要介紹了Vuejs如何通過(guò)Axios請(qǐng)求數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Vue實(shí)現(xiàn)成績(jī)?cè)鰟h案例思路詳解

    Vue實(shí)現(xiàn)成績(jī)?cè)鰟h案例思路詳解

    這篇文章主要介紹了Vue實(shí)現(xiàn)成績(jī)?cè)鰟h案例思路詳解,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2025-05-05
  • Vue中的計(jì)算屬性和axios基本使用回顧

    Vue中的計(jì)算屬性和axios基本使用回顧

    這篇文章主要介紹了Vue中的計(jì)算屬性和axios基本使用回顧,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01

最新評(píng)論

砀山县| 东光县| 焦作市| 柘荣县| 买车| 晋江市| 门源| 三江| 盖州市| 太和县| 宜丰县| 延津县| 衡南县| 保德县| 梅河口市| 宽城| 深州市| 霍林郭勒市| 正镶白旗| 漳州市| 德化县| 陵川县| 临猗县| 吴堡县| 同仁县| 英吉沙县| 尖扎县| 南雄市| 琼海市| 明光市| 焉耆| 夏河县| 定边县| 兴国县| 房山区| 石渠县| 兰西县| 盐池县| 漳浦县| 武胜县| 丰原市|