目录结构
安装ejs
cnpm install egg-view-ejs --save
ejs配置
//config/plugin.js
'use strict';exports.ejs = { enable: true, package: 'egg-view-ejs', };
//config/config.default
'use strict';module.exports = appInfo => { const config = exports = {}; // use for cookie sign key, should change to your own and keep security config.keys = appInfo.name + '_1537172517005_2221'; // add your config here config.middleware = []; config.view = { mapping: { '.html': 'ejs', }, }; return config;};
controller
//constroller/news.js
'use strict';const Controller = require('egg').Controller;class NewsController extends Controller { async index() { let name = this.ctx.query.name; let id = this.ctx.params.id; let list = [1,2,3]; await this.ctx.render('news',{ name, id, list }) }}module.exports = NewsController;
router
//router.js
router.get('/news', controller.news.index); router.get('/news/:id', controller.news.index);
view
//news.html
Document query:<%= name %>
params<%= id %>
- <% for(var i=0;i
- <%= list[i]%> <% } %>
get,query传值
get,params传值
静态资源