javascript - koa-router 接受post请求如何让页面跳转

查看:339
本文介绍了javascript - koa-router 接受post请求如何让页面跳转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

使用koa-router管理路由的时候,想在收到表单post请求后redirect到别的路由,但是使用this.redirect和router.redirect都无法跳转。请问正确的写法是什么
无效的代码:

router.post('/login', bodyParser, function* (next) {
  // ...
  this.redirect('/index');
});

解决方案

router.get('user', '/users/:id', function *(next) {
 // ...
});

router.url('user', 3);
// => "/users/3"

koa-router 文档里写的方法,类似的还有 router.redirect

你也可以直接写进 this 里面,来符合你的习惯

这篇关于javascript - koa-router 接受post请求如何让页面跳转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆