• vue+electron一键入门


    前言

    帮公司弄了一个vue+electron项目,里面用到了axios、element-ui、ue-router、js-md5、sqlite3这些依赖库,其中sqlite3比较难搞下面会详细展开来讲,同时也涉及打包(window包、mac包)

    开始

    其实项目整体没啥好讲,我就讲一下数据库的封装、打包配置注意事项即可

    sqlite3数据库使用

    ps: 你要有python2.7版本

    npm install sqlite3 --save

    具体安装步骤你可以百度或者参考这篇文章

    安装完成之后,你还要配置来到vue.config.js文件,开启node环境

    1. module.exports = {
    2. pluginOptions:{
    3. electronBuilder:{
    4. nodeIntegration:true
    5. }
    6. }
    7. }

    简单封装的db.js

    时间戳小技巧main.js

    1. Date.prototype.Format = function (fmt) {
    2. var o = {
    3. "M+": this.getMonth() + 1, //月份
    4. "d+": this.getDate(), //日
    5. "h+": this.getHours(), //小时
    6. "m+": this.getMinutes(), //分
    7. "s+": this.getSeconds(), //秒
    8. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    9. "S": this.getMilliseconds() //毫秒
    10. };
    11. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    12. for (var k in o)
    13. if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    14. return fmt;
    15. }
    1. let sqlite3 = require('sqlite3').verbose();
    2. const path = require('path');
    3. let basePath = process.env.NODE_ENV !== 'development' ? path.dirname(process.execPath) : process.cwd();
    4. let dbPath = path.join(basePath, '/dataBase/table.db');
    5. console.log('数据库路径: ', dbPath);
    6. const db = new sqlite3.Database(dbPath);
    7. // 启动软件时判断是否有db文件,没有就创建db文件,同时创建表...看个人需求自行完成
    8. // db.all("select * from province_list", (err, res) => {
    9. // console.log(JSON.stringify(res))
    10. // })
    11. export default db
    12. // 使用1
    13. // 1.main.js 引入注册
    14. import db from './db'
    15. Vue.prototype.$db = db
    16. // 2.index.vue文件使用
    17. this.$db.all("select * from province_list", (err, res) => {
    18. console.log(JSON.stringify(res))
    19. })
    20. // 使用2
    21. // 1.index.vue文件使用
    22. import modelHome from '/db/api/home'
    23. modelHome.test()
    24. // 2./db/aip/home/index.js
    25. import db from '../db'
    26. test() {
    27. return new Promise((resolve, reject) => {
    28. db.all("select * from task_list", (err, res) => {
    29. // console.log(res)
    30. if (err) {
    31. reject(err);
    32. } else {
    33. resolve(res);
    34. }
    35. })
    36. })
    37. },
    38. __addTask(item) {
    39. item.update_time = new Date().Format("yyyy/MM/dd hh:mm:ss")
    40. item.crate_time = new Date().Format("yyyy/MM/dd hh:mm:ss")
    41. return new Promise((resolve, reject) => {
    42. db.all(`INSERT INTO task_list VALUES (NULL, "${item.name}", "${item.linkUrl}", "${item.ua_type}", "${item.plan_open_num}", "${item.window_num}", '${item.proxy_config}',"${item.update_time}", "${item.crate_time}")`, (err, res) => {
    43. if (err) {
    44. reject(err)
    45. } else {
    46. resolve(res)
    47. }
    48. })
    49. })
    50. },
    51. __updateTask(item) {
    52. item.update_time = new Date().Format("yyyy/MM/dd hh:mm:ss")
    53. return new Promise((resolve, reject) => {
    54. db.all(`UPDATE task_list SET name="${item.name}", linkUrl="${item.linkUrl}", ua_type="${item.ua_type}", plan_open_num="${item.plan_open_num}", window_num="${item.window_num}", proxy_config='${item.proxy_config}', update_time="${item.update_time}" WHERE id="${item.id}"`, (err, res) => {
    55. if (err) {
    56. reject(err)
    57. } else {
    58. resolve(res)
    59. }
    60. })
    61. })
    62. },
    63. __dateltTask(item) {
    64. return new Promise((resolve, reject) => {
    65. db.all(`DELETE FROM task_list WHERE id="${item.id}"`, (err, res) => {
    66. if (err) {
    67. reject(err)
    68. } else {
    69. resolve(res)
    70. }
    71. })
    72. })
    73. },

    数据库知识我们就讲完了,注意点如果你没有自动生成db文件,你打包之后需要手动复制db文件到安装软件的目录里面。

    打包

    1.隐藏help、file这些菜单

    src/background.js

    1. import { app, protocol, BrowserWindow, Menu } from 'electron'
    2. app.on('ready', async () => {
    3. createWindow()
    4. // 隐藏菜单栏
    5. Menu.setApplicationMenu(null);
    6. })

    2.修改窗口标题名

    public/index.html

    1. html>
    2. <html lang="">
    3. <head>
    4. <meta charset="utf-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width,initial-scale=1.0">
    7. <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    8. <title>测试标题名title>
    9. head>
    10. <body>
    11. <noscript>
    12. <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.strong>
    13. noscript>
    14. <div id="app">div>
    15. body>
    16. html>

    3.打包配置修改各种东西

    vue.config.js

    1. module.exports = {
    2. pluginOptions:{
    3. electronBuilder:{
    4. nodeIntegration:true,
    5. // 打包配置参数
    6. builderOptions: {
    7. productName: "测试应用", // 应用的名称
    8. appId: 'www.xxx.com', // 项目唯一标识
    9. copyright: 'Copyright © xxx',
    10. directories: {
    11. output: "build_electron" // 输出文件夹
    12. },
    13. electronDownload: {
    14. mirror: "https://npm.taobao.org/mirrors/electron/" //镜像设置
    15. },
    16. win: {
    17. icon: './src/assets/logo.ico', //打包windows版本的logo
    18. target: [{
    19. target: 'nsis', // 利用nsis制作安装程序
    20. 'arch': [
    21. 'x64', // 64位
    22. 'ia32'
    23. ]
    24. }]
    25. },
    26. nsis: {
    27. oneClick: false, // 一键安装
    28. perMachine: true, // 是否开启安装时权限限制(此电脑或当前用户)
    29. allowElevation: true, // 允许请求提升。 如果为false,则用户必须使用提升的权限重新启动安装程序。
    30. allowToChangeInstallationDirectory: true, // 允许修改安装目录
    31. installerIcon: "./src/assets/logo.ico", // 安装图标
    32. installerHeaderIcon: "./src/assets/logo.ico", // 安装时头部图标
    33. uninstallerIcon: "./src/assets/logo.ico", // 卸载图标
    34. createDesktopShortcut: true, // 创建桌面图标
    35. createStartMenuShortcut: true, // 创建开始菜单图标
    36. },
    37. }
    38. },
    39. }
    40. }

    4.打包window包和mac包

    package.json

    1. "scripts": {
    2. "e:build:mac": "vue-cli-service electron:build --mac",
    3. "e:build": "vue-cli-service electron:build",
    4. "e:serve": "vue-cli-service electron:serve"
    5. }
    1. npm run e:build
    2. // window包
    3. npm run e:build:mac
    4. // mac包

    注意mac包需要在mac电脑才能打,同时第一次打包的时候需要开启科学上网(它会自动下载electron-builder依赖),第一次打包成功之后 第二次就会快很多。

    总结

    1.sqlite3数据库安装、使用

    2.打包配置,注意点就是要开启科学上网

  • 相关阅读:
    在window10脱离hadoop独立安装hbase-2.5.6
    线上服务异常的定位、处理与优化的探索 - 第三章 Java虚拟机
    软件系统开发安全指南-word
    [Java]前中后序遍历二叉树/递归与非递归
    基于最低水平面的三维装箱问题的启发式算法
    练气第六天
    CENTOS上的网络安全工具(十一)走向Hadoop(3) MapReduce示例解析
    vue简单源码手写,实现基本的模板解析,v-text,v-html,v-on:click,@click基本语法指令
    js中的sort排序
    ThinkPHP 3.2 常用内置函数
  • 原文地址:https://blog.csdn.net/echo_Ae/article/details/132868782