admin管理员组文章数量:1355748
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
2 Answers
Reset to default 7I 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!
本文标签: javascriptProperty 39slug39 does not exist on type 39ParsedUrlQueryundefined39Stack Overflow
版权声明:本文标题:javascript - Property 'slug' does not exist on type 'ParsedUrlQuery | undefined' - Stack Overflo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743987280a2571497.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论