1.安装 ejs
yarn add egg-view-ejs
2.修改配置
config/plugin.js
'use strict';
exports.ejs = {
enable: true,
package: 'egg-view-ejs'
};
config/config.default.js
config.view = {
mapping: {
'.html': 'ejs'
}
};
3.创建 html 文件
app/view/index.html
首页这是首页内容
id:
name:
4.传值
app/controller/home.js
'use strict';
const Controller = require('egg').Controller;
class HomeController extends Controller {
async index() {
const { ctx } = this;
const res = await ctx.service.product.index();
// ctx.body = res;
await ctx.render('index.html', {
res,
lists: ['a', 'b', 'c']
});
}
}
module.exports = HomeController;
5.效果