请使用2.0版本
1. 选择开发模式
打开配置文件:electron-egg/electron/config.js,可修改如下配置:
developmentMode: {
default: 'vue', // 默认前后端分离,使用vue
mode: {
// 前后端分离,使用vue开发,端口与vue启动的serve一致
vue: {
hostname: 'localhost',
port: 8080
},
// 前后端分离,使用react开发,端口与react启动的serve一致
react: {
hostname: 'localhost',
port: 3000
},
// ejs模板渲染
ejs: {
hostname: 'localhost',
port: 7068 // The same as the egg port
}
}
},
2. 启动
(1) vue模式【默认】
# 1:【进入前端目录】,启动vue
cd electron-egg/frontend
npm run serve
# 2:【根目录】,启动electron服务
npm run dev
(2) react模式,同vue
1:frontend目录为前端目录,理论上可使用任何前端技术
(3) ejs模式,模板渲染
# 直接启动桌面应用即可
# 根目录
npm run dev
3. 编写一个api,供前端使用
- 创建路由
在electron-egg/app/router/index.js文件中,添加:
router.get('/hello', controller.v1.home.hello);
在控制器层中(electron-egg/app/controller/v1/home.js),编写方法
async hello() {
const { ctx, service } = this;
const data = {
title: 'hello'
};
this.sendSuccess(data);
}
访问api
http://localhost:7068/hello
4. 如果是ejs模板渲染方式,编写一个hello页面
创建路由
在electron-egg/app/router/index.js文件中,添加:
router.get('/helloPage', controller.v1.home.helloPage);
创建electron-egg/app/view/hello.ejs视图文件
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1><%= title %></h1>
<p>Welcome to electron-egg</p>
</body>
</html>
添加方法
在electron-egg/app/controller/v1/home.js文件中,添加方法:
async helloPage() {
const { ctx } = this;
const data = {
title: 'hello'
};
await ctx.render('hello.ejs', data);
}
注:egg详细功能,请参照egg框架文档学习即可
5. 打包成exe、dmg、deb可执行文件
见目录