Nextjs-如何使用Link将参数传递到服务器路由器

时间:2018-08-25 13:45:38

标签: reactjs next.js

我有一个链接DOM,例如<Link href="/blog/0", as="/blog/ss" /> 我的服务器路由器就是这样。

router.get('/blog/:articleId', async (ctx) => {
  await app.render(ctx.req, ctx.res, '/articles', { articleId: ctx.params.articleId });
  ctx.respond = false;
});

如何获取链接的href值?我得到的ctx.params.articleIdss

更新: 请求对象

{ request:
   { method: 'GET',
     url: '/articles/test-context-1',
     header:
      { host: '127.0.0.1:3000',
        connection: 'keep-alive',
        'upgrade-insecure-requests': '1',
        dnt: '1',
        'save-data': 'on',
        'user-agent':
         'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6)     AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
    accept:
     'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    referer: 'http://127.0.0.1:3000/',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7' } },
  response: { status: 200, message: 'OK', header: {} },
  app: { subdomainOffset: 2, proxy: false, env: 'development' },
  originalUrl: '/articles/test-context-1',
  req: '<original node req>',
  res: '<original node res>',
  socket: '<original node socket>' }

1 个答案:

答案 0 :(得分:0)

应为ctx。要求 params.articleId。

您错过了对象中的req

router.get('/blog/:articleId', async (ctx) => {
  await app.render(ctx.req, ctx.res, '/articles', { articleId: ctx.req.params.articleId });
  ctx.respond = false;
});