electron-vite-vue/README.zh-CN.md

97 lines
2.9 KiB
Markdown
Raw Normal View History

2021-11-09 19:38:57 +08:00
# electron-vue-vite
![GitHub stars](https://img.shields.io/github/stars/caoxiemeihao/electron-vue-vite?color=fa6470&style=flat)
![GitHub forks](https://img.shields.io/github/forks/caoxiemeihao/electron-vue-vite?style=flat)
![GitHub issues](https://img.shields.io/github/issues/caoxiemeihao/electron-vue-vite?style=flat)
![GitHub license](https://img.shields.io/github/license/caoxiemeihao/electron-vue-vite?style=flat)
2021-11-10 20:21:51 +08:00
**[English](README.md) | 简体中文**
2021-11-09 19:38:57 +08:00
🥳 基于 `Rollup`、`⚡Vite` 构建的 `Electron` + `Vue3` + `Vite2` 模板;**简单,容易上手!**
2021-11-09 19:56:52 +08:00
## Run Setup
```bash
# clone the project
git clone git@github.com:caoxiemeihao/electron-vue-vite.git
# enter the project directory
cd electron-vue-vite
# install dependency(Recommend use yarn)
yarn
# develop
yarn dev
```
2021-11-11 18:29:18 +08:00
## 目录结构
```tree
├── configs
├ ├── vite-main.config.ts 主进程配置文件,编译 src/main
├ ├── vite-preload.config.ts 预加载脚本配置文件,编译 src/preload
├ ├── vite-renderer.config.ts 渲染进程配置文件,编译 src/renderer
├── scripts
├ ├── build.mjs 项目构建脚本,对应 npm run build
├ ├── electron-builder.config.mjs
├ ├── watch.mjs 项目开发脚本,对应 npm run dev
├── src
├ ├── main 主进程源码
├ ├── preload 预加载脚本源码
├ ├── renderer 渲染进程源码
```
2021-11-09 19:38:57 +08:00
2021-11-09 19:50:11 +08:00
#### dist 与 src
- 一旦启动或打包脚本执行过,会在根目录产生 **`dist` 文件夹,里面的文件夹同 `src` 一模一样**
2021-11-11 18:29:18 +08:00
- 在使用一些路径计算时,尤其是相对路径计算;`dist` 与 `src` 里面保持相同的目录结构能避开好多问题
2021-11-09 19:50:11 +08:00
```tree
├── dist
| ├── main
| ├── preload
2021-11-11 18:29:18 +08:00
| ├── renderer
2021-11-09 19:50:11 +08:00
├── src
| ├── main
| ├── preload
2021-11-11 18:29:18 +08:00
| ├── renderer
2021-11-09 19:50:11 +08:00
|
```
2021-11-09 19:38:57 +08:00
## 渲染进程使用 NodeJs API
2021-11-09 19:56:52 +08:00
- 因为安全的原因 Electron 默认不支持在 渲染进程 中使用 NodeJs API
(如果你执意要在 渲染进程 中使用 NodeJs API 那请自便)
2021-11-09 19:38:57 +08:00
2021-11-09 19:56:52 +08:00
- 推荐所有的 NodeJs、Electron API 通过 `preload-script` 注入到 渲染进程中,例如:
2021-11-09 19:38:57 +08:00
2021-11-11 18:29:18 +08:00
* **src/preload/index.ts**
2021-11-09 19:38:57 +08:00
2021-11-11 18:29:18 +08:00
```typescript
// --------- Expose some API to Renderer process. ---------
contextBridge.exposeInMainWorld('fs', fs)
contextBridge.exposeInMainWorld('ipcRenderer', ipcRenderer)
```
2021-11-09 19:38:57 +08:00
2021-11-11 18:29:18 +08:00
* **src/renderer/main.ts**
2021-11-09 19:38:57 +08:00
2021-11-11 18:29:18 +08:00
```typescript
console.log('fs', window.fs)
console.log('ipcRenderer', window.ipcRenderer)
```
2021-11-09 19:38:57 +08:00
## 运行效果
<img width="700px" src="https://raw.githubusercontent.com/caoxiemeihao/blog/main/electron-vue-vite/screenshot/electron-15.png" />
2021-11-09 19:56:52 +08:00
## 微信群
2021-11-09 19:38:57 +08:00
2021-11-10 20:21:51 +08:00
<img width="244px" src="https://raw.githubusercontent.com/caoxiemeihao/blog/main/assets/wechat/group/qrcode.jpg" />