Configuration|Configuration method of electron+vue+ts+sqlite
This article mainly introduces the configuration method of |electron vue ts sqlite. It has a certain reference value. Now I share it with you. Friends in need can refer to it.
From the perspective of programming model It is said that using a declarative language to declare styles and layouts and using a full-featured programming language to write business logic is a best practice for GUI programs.
I have to write a personal project recently, so I naturally thought of using the front-end to write the interface. Through electron, you can use front-end technology to develop desktop programs. It is actually equivalent to embedding a webkit browser core, but with some tailoring and optimization.
In addition, the front-end framework uses Vue, which I am familiar with. Both the interface code and the core code are written in typescript. Its static type system is very powerful and combines the advantages of static and dynamic languages.
Initialization configuration
Initialize vue and typescrpt
npm install --global @vue/cli # 2. 创建一个新工程,并选择 "Manually select features (手动选择特性)" 选项 vue create idocumentation
Check typescript, others as needed Check.
Vue CLI v3.0.0-rc.3 ? Please pick a preset: Manually select features ? Check the features needed for your project: Babel, TS, Router, Vuex, Linter ? Use class-style component syntax? No # 是否使用class风格的组件定义 ? Use Babel alongside TypeScript for auto-detected polyfills? (Y/n)Yes ? Pick a linter / formatter config: TSLint ? Pick additional lint features: Lint on save ? Where do you prefer placing config for Babel, PostCSS, ESLint, etc.? In dedicated config files # 分离配置文件
In this way, the vue scaffolding will automatically help you initialize the structure of the typescript vue project. You can go in and take a look.
TSLint will check the format and specifications of the code, help you standardize the format, and also help you avoid bugs caused by bad habits.
However, the default configuration is a bit strict. This can be done by modifying tslint.json
. The following is my configuration:
"rules": { "quotemark": [false, "single"], "indent": [true, "spaces", 2], "interface-name": false, "ordered-imports": false, "object-literal-sort-keys": false, "no-consecutive-blank-lines": false, "eofline": false, "prefer-const": false, "no-console": false, "trailing-comma": false, "max-classes-per-file": false }
If you think a certain check is too strict, you can Turn off, refer to here for specific fields: https://palantir.github.io/ts...
Install configuration electron
First install:
npm install -g electron # 全局安装方式 npm install electron # 本地安装方式 推荐
Then write electron
The entry code of the main process, here is a reference, put it in main.js
in the project root directory:
https://github.com/electron/ e...
Notice that there is a line:
mainWindow.loadFile('index.html')
This is the front-end page file loaded when electron starts. Of course, you can also let electron load from the url, just like browsing The same as opening the server:
mainWindow.loadURL('http://localhost:8080');
The general workflow is to use the vue development server to start the vue development server, and the vue development server will listen on port 8080.
The server will monitor file system events. When the project code is modified, it will recompile and package.
So, when developing, it is more convenient to let electron load the main page from vue's development server.
Finally add under package.json
:
"main": "main.js", // ... "scripts": { "electron": "node node_modules/electron/cli.js ." },
Among them, the main
field specifies the entry file of the project, which is the ## just written #main.js.
scriptsThe meaning of the configuration is that when
npm run electron is run in the terminal, it will be executed:
node node_modules/electron/cli.js .
DebuggingFirst, execute in the terminal:
npm run serve
http://127.0.0.1:8080 in the browser at this time, you can also access it normally, but it is best to debug in electron. Because the electron project may involve calls to operating system-related libraries such as fs, using the browser is not supported.
npm run electron
- First the vue project you wrote is detected by the vue development server
- The development server will Compile and package
- electron accesses the vue development server and gets the web page and code, similar to a browser
- The web page and code are in the electron environment Rendering and execution
- The library needs to be packaged when vue’s development server is packaged Package it in
- The electron environment needs to support the operation of the library
The solution is to modify the configuration of webpack, edit
vue.config.js, the content is:
module.exports = { configureWebpack: { target: "electron-renderer" } }
It took me all afternoon, but I haven’t completely solved this problem yet. If anyone has a good solution, please tell me, thank you!
The first problem is that since sqlite is written in C, it will encounter compilation and linking problems during installation.
If you directly:
npm i --save sqlite
这里提到,这不是bug,这是feature:https://github.com/mapbox/nod...
可能的解决方案
方案一
是不行的!你还需要将sqlite的二进制库文件链接到electron的二进制文件上去,对的,就像你配置C或C++程序那样恐怖。好在有现成的工具,执行:
npm i --save-dev electron-rebuild ./node_modules/.bin/electron-rebuild
它会重新编译链接electron。至于能不能成功,看运气吧。
我在Windows下尝试了下,一会说需要python环境,一会网络链接又不行要下什么预编译包,总之事情很多。
后来在linux环境下尝试了,成功了。之后在electron的主进程里,也就是前面说的main.js入口文件中,尝试了下发现可以使用。
方案二
方案一解决了问题一。那么还有问题二没有解决。
我们梳理下现在手上的问题:
sqlite库能够在electron主进程运行。
sqlite库由于webapck的原因无法在渲染进程中运行。
那么,一种很自然而然的想法是,让实际的sqlite调用在主进程执行,渲染进程通过IPC方式和主进程通信。
如果把这种过程封装起来,即渲染进程中调用某个包装类来调用sqlite3,而包装类会将对应的调用信息通过IPC发送给主进程,主进程真正调用sqlite3模块来完成操作。
这种方式封装了就是远程过程调用(RMI)了,如果需求不高也可以不封装。
方案三
方案三则是用其它的替代思路了。有一个叫做sql.js
的库,也能够操作sqlite。
这个库很有意思,它纯粹用js实现的。怎么做到的?性能能好吗?
准确的说不是js。这个库不是手写的代码,它是使用Emscripten将sqlite的C语言实现编译成asm.js。
而asm.js是一个js的严格子集,模型上和C更能对应上去,一旦js引擎发现运行的是asm.js,就跳过语法分析直接转为汇编语言。
但是它有几个缺点:
只能操作内存中的数据库,无法操作文件系统
性能和原生实现的sqlite相比,很显然,不高
如果在electorn中要拿它暂时用一用,则需要把数据库完全读入到内存中操作。处理不好,内存会爆炸的。
好在我这里需要用的sqlite只是存存元数据,几十k大小,还是能勉强用用到。先临时用这个顶上,封装一层,写后续的代码。前端和node发展很快,等以后有人弄出easy的解决方案了,再切换回sqlite模块。
方案四
方案四则是看看能不能改下项目的技术选型,要不换个其它的嵌入式数据库?
最后
electron的优点在于大大降低了开发成本,本身前端的方式开发界面就是一种良好的实践的,而前端蓬勃发展的今天又有大量的框架和组件库可供直接调用。
记得大学里写过GTK和Qt的图形界面,对比之下,传统的Qt写界面非常麻烦费事,而且也远远没有前端漂亮,动态性也差一大截。
不过electron的缺点在于打包后体积太大,而且运行性能不高。不过一般的场景中,这点缺点问题不大。
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
The above is the detailed content of Configuration|Configuration method of electron+vue+ts+sqlite. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





Title: The working principle and configuration method of GDM in Linux systems In Linux operating systems, GDM (GNOMEDisplayManager) is a common display manager used to control graphical user interface (GUI) login and user session management. This article will introduce the working principle and configuration method of GDM, as well as provide specific code examples. 1. Working principle of GDM GDM is the display manager in the GNOME desktop environment. It is responsible for starting the X server and providing the login interface. The user enters

Understanding Linux Bashrc: Function, Configuration and Usage In Linux systems, Bashrc (BourneAgainShellruncommands) is a very important configuration file, which contains various commands and settings that are automatically run when the system starts. The Bashrc file is usually located in the user's home directory and is a hidden file. Its function is to customize the Bashshell environment for the user. 1. Bashrc function setting environment

Title: How to configure and install FTPS in Linux system, specific code examples are required. In Linux system, FTPS is a secure file transfer protocol. Compared with FTP, FTPS encrypts the transmitted data through TLS/SSL protocol, which improves Security of data transmission. In this article, we will introduce how to configure and install FTPS in a Linux system and provide specific code examples. Step 1: Install vsftpd Open the terminal and enter the following command to install vsftpd: sudo

When we use win11 system, we sometimes need to check the configuration of our computer, but many users are also asking where to check the configuration of win11 computer? In fact, the method is very simple. Users can directly open the system information under settings, and then view the computer configuration information. Let this site carefully introduce to users how to find win11 computer configuration information. How to find win11 computer configuration information. Method 1: 1. Click Start and open Computer Settings. 3. You can view computer configuration information on this page. 2. In the command prompt window, enter systeminfo and press Enter to view the computer configuration.

Teach you step by step how to configure Maven local warehouse: improve project construction speed Maven is a powerful project management tool that is widely used in Java development. It can help us manage project dependencies, build projects, and publish projects, etc. However, during the actual development process, we sometimes encounter the problem of slow project construction. One solution is to configure a local repository to improve project build speed. This article will teach you step by step how to configure the Maven local warehouse to make your project construction more efficient. Why do you need to configure a local warehouse?

PyCharm is a powerful Python integrated development environment that allows developers to write, debug and manage Python code more efficiently. In the daily development process, we often encounter situations where environment variables need to be configured so that the program can correctly access the required resources. This article will introduce in detail how to configure environment variables in PyCharm and provide specific code examples. 1. Configure PyCharm’s environment variables. Configuring environment variables in PyCharm is very simple. The following are the specific steps:

Types of Linux log files and configuration methods In Linux systems, log files are very important. They record the running status of the system, user operations, and the occurrence of various events. By checking log files, system administrators can discover problems in time and handle them accordingly. This article will introduce the common types of log files in Linux systems and how to configure logging. 1. Types of log files System log: System log is a log file that records the operating status of the system, including system startup, shutdown, service startup and stop, etc.

The game Black Myth Wukong will be launched on all major platforms in the summer of 2024. Players need to meet certain computer configurations when downloading the game to experience it. The following is an introduction to the minimum configuration required for Black Myth Wukong. What computer configuration is required for Black Myth Wukong? Minimum configuration operating system: Windows 7, Windows 8.1, Windows 10 (all 64-bit) Processor: Intel Corei5-4430/AMDFX-6300 Running memory: 8GB RAM Graphics card: NVIDIA GeForce GTX9602GB/AMDRadeon R73702GB Storage space: 100GB required Available space recommended operating system: Windows 7, Win
