简体   繁体   中英

Nextjs - How to pass params to server router with Link

I have a Link DOM such as <Link href="/blog/0", as="/blog/ss" /> My server router is like this.

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

How could I get the Link's href value? The ctx.params.articleId which I get is ss .

Update: the request object

{ 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>' }

It should be ctx. req. params.articleId.

You missed req in your object.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM