最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Property 'slug' does not exist on type 'ParsedUrlQuery | undefined' - Stack Overflo

programmeradmin1浏览0评论

I am trying to get the path of my page on getServerSideProps, but the type of params is object

What I need to do to get as string?

export const getServerSideProps: GetServerSideProps = async (context) => {
  const { slug } = context.params // Property 'slug' does not exist on type 'ParsedUrlQuery | undefined'.ts(2339)

  return {
    props: {
    },
  }
}

I am trying to get the path of my page on getServerSideProps, but the type of params is object

What I need to do to get as string?

export const getServerSideProps: GetServerSideProps = async (context) => {
  const { slug } = context.params // Property 'slug' does not exist on type 'ParsedUrlQuery | undefined'.ts(2339)

  return {
    props: {
    },
  }
}
Share Improve this question asked Oct 27, 2021 at 16:13 RodrigoRodrigo 54115 gold badges76 silver badges162 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

I think that you look for code that look like this

import { ParsedUrlQuery } from "querystring";

interface Params extends ParsedUrlQuery {
  slug: string;
}

export const getServerSideProps: GetServerSideProps = async (context) => {
  const { slug } = context.params as Params;

  return {
    props: {
    },
  }
}

If you're looking for the path as a String, you can try context.req.url. (See the docs for the full list of context properties).

For the dynamic parameter, it depends on what you've named the page, for example if you have a Blog post page, with a pages/posts/[post].js file, the slug will be in context.params.post as opposed to context.params.slug.

Hope this is helpful!

发布评论

评论列表(0)

  1. 暂无评论