记一次vueCli3.0项目打包exe

注意点,登录存token不能放在cookies里面要放在vuex

路由要采用hash模式路由不能采用history模式

安装包为了加快速度 npm config edit ,在打开的文件里加入

electron_mirror=https://npm.taobao.org/mirrors/electron/
electron-builder-binaries_mirror=https://npm.taobao.org/mirrors/electron-builder-binaries/

 调试代码 ctrl+shift + i

修改 permission.js ,user.js(我自己的项目)

 

package.json  文件

 "main": "background.js",

 "scripts": {

"electron:build": "vue-cli-service electron:build",
"electron:serve": "vue-cli-service electron:serve",
"postinstall": "electron-builder install-app-deps",
"postuninstall": "electron-builder install-app-deps"
  },
 "dependencies": {
 "is-electron": "^2.2.0",
},
 "devDependencies": {
   "electron": "^9.0.0",
    "electron-devtools-installer": "^3.1.0",
    "vue-cli-plugin-electron-builder": "~2.0.0-rc.4",
}
 
background.js 文件(放入src文件夹里面)
import {
    app,
    protocol,
    BrowserWindow,
    globalShortcut,
    ipcMain,
} from 'electron'
import {
    createProtocol,
    installVueDevtools,
} from 'vue-cli-plugin-electron-builder/lib'


const isDevelopment = process.env.NODE_ENV !== 'production'
const ipc = ipcMain



// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win

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

function createWindow() {
    // Create the browser window.
    win = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
            nodeIntegration: true,
        },
        frame: true
    })

    if (process.env.WEBPACK_DEV_SERVER_URL) {
        // Load the url of the dev server if in development mode
        win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)

    } else {
        createProtocol('app')
        // Load the index.html when not in development
        win.loadURL('app://./index.html')
        console.log(process.env.WEBPACK_DEV_SERVER_URL)
    }
    // 当应用所有窗口关闭要做的事情
    win.on('closed', () => {
        win = null
    })
    /*隐藏electron菜单栏*/
    win.setMenu(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 (win === null) {
        createWindow()
    }
})

//当应用启动后(初始化完成)要做的一些事情
app.on('ready', async () => {
    if (win == null) {
        createWindow()
    }
    // 通过快捷键就可以打开调试模式 ctrl + shift + i
    globalShortcut.register('CommandOrControl+shift+i', () => {
        const focusWin = BrowserWindow.getFocusedWindow()
        focusWin && focusWin.toggleDevTools()
    })
})

// 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()
        })
    }
}

//登录窗口最小化
ipc.on('min', function () {
    win.minimize();
})
//登录窗口最大化
ipc.on('max', function () {
    win.maximize();
})
//登录窗口还原
ipc.on('unmaximize', function () {
    win.restore();

})
ipc.on('close', function () {
    win.close();
})
 
 
posted @ 2020-11-05 17:19  絮絮墨恒  阅读(402)  评论(0编辑  收藏  举报