类型“ParsedUrlQuery | ”上不存在属性“slug” 不明确的'

Skh*_*haz 3 javascript typescript next.js

我试图获取我的页面的路径getServerSideProps,但类型params对象

我需要做什么才能得到字符串?

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

  return {
    props: {
    },
  }
}
Run Code Online (Sandbox Code Playgroud)

小智 7

我认为您正在寻找类似这样的代码

import { ParsedUrlQuery } from "querystring";

interface Params extends ParsedUrlQuery {
  slug: string;
}

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

  return {
    props: {
    },
  }
}
Run Code Online (Sandbox Code Playgroud)