egg-view-nunjucks配置
/config/plugin.js
// 模板引擎exports.nunjucks = {enable: true,package: 'egg-view-nunjucks',};
config.defaut.js
/config/config.default.js
对 view视图进行配置
mapping.html 如果文件以 .html结尾,就用 nunjucks模板引擎
// 对 view视图进行配置config.view = {mapping: {'.html': 'nunjucks',},defaultViewEngine: 'nunjucks',// root: path.join(appInfo.baseDir, 'app/html'),, // [].join(',')};// 对 模板引擎进行配置config.nunjucks = {}
Controller中使用模板引擎
ctx.render(‘index.html’)
- ctx.render返回 Promise,前面要加上 await
 - /app/view目录下要有 index.html文件,这个文件遵循 nunjucks模板的语法 ```jsx ‘use strict’; const { Controller } = require(‘egg’);
 
class HomeController extends Controller { async index() { const { ctx } = this;
const res = await ctx.service.sku.index();// ctx.body = res;// 必须要加 await,否则路由 404await ctx.render('index.html', res);
} }
module.exports = HomeController; ```
