1.安装 egg-view-handlebars
2.在config目录下的plugin,js中配置
exports.handlebars = {
enable: true,
package: 'egg-view-handlebars',
};
3.在config.default.js下配置
config.view = {
root: [
path.join(appInfo.baseDir, 'app/view'),
].join(','),
defaultViewEngine: 'handlebars',
defaultExtension: '.hbs',
mapping: {
'.hbs': 'handlebars',
}
};
4.在controller层使用handelbars渲染对应页面
class HomeController extends Controller {
async index() {
const { ctx } = this;
ctx.body = 'hi, egg';
}
async news() {
const { ctx } = this;
let a={name:"andong"}
await ctx.render('home.hbs',a);
}
}
网友评论