【发布时间】:2018-09-25 09:14:50
【问题描述】:
我是 koa@2 的新手,我正在尝试重定向到特定页面。我能够重定向到静态页面,但我想向该页面发送一些数据以显示。我搜索了执行此操作的方法,发现 ctx(Koa Context) 有一个方法 render(),它需要两个 args(1st-page url, 2nd - locals(our data))。
我试过了:
const Koa = require('koa');
const render = require('koa-ejs');
const path = require('path');
const app = new Koa();
render(app, {
root: path.join(__dirname, 'view'),
layout: 'template',
viewExt: 'html',
cache: false,
debug: true
});
app.use(async function (ctx) {
await ctx.render('user');
});
app.listen(7001);
还有这个:
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);
});
但我总是得到属性渲染在 ctx 上不存在。 我尝试将 ctx 的类型从 BaseContex 更改为 IRouter,但发布的内容保持不变。 我觉得我需要将render属性附加到ctx,但我不知道该怎么做。
有人可以帮我解决这个问题吗?
【问题讨论】:
标签: node.js typescript redirect koa koa2