What's the recommend code for koa-ejs using koa2?

﹥>﹥吖頭↗ 提交于 2019-12-25 07:41:42

问题


I planed use ejs in koa2, my codes were like as blelow:

render(app, {
  root: path.join(__dirname, 'views-ejs'),
  layout: 'layout',
  viewExt: 'ejs',
  cache: false,
  debug: true
});

app.use(function *() {
  yield this.render('index',{
    title: 'koa2 title',
    viewClass: 'landing',
    targetAuthLevel:'',
    authorizationLevel:'6',
    ngController: 'landingController'
  });
});

But, I get the below warning, would you tell me what's codes are recommended? please.

koa deprecated Support for generators will been removed in v3. See the documentation for examples of how to convert old middleware https://github.com/koajs/koa/tree/v2.x#old-signature-middleware-v1x


回答1:


Per the README:

Workaround for Koa 2

npm install co --save

Then...

import co from 'co';
import render from 'koa-ejs';

render(app, options);
app.context.render = co.wrap(app.context.render);

app.use(async (ctx, next) => {
    await ctx.render(view, locals);
});



回答2:


You can avoid this error with updating koa-ejs to "next" version:

$ npm rm -S koa-ejs
$ npm i -S koa-ejs@next
  • rm alias for remove command
  • i alias for install command
  • -S alias for --save key


来源:https://stackoverflow.com/questions/36976834/whats-the-recommend-code-for-koa-ejs-using-koa2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!