getServersideProps访问当前浏览器URL |
您所在的位置:网站首页 › 获取当前view › getServersideProps访问当前浏览器URL |
问题描述
我正在调用getServersideProps并通过此类参数传递: export async function getServerSideProps({ req, res }) {}我需要获取当前的浏览器URL路径,并且在请求对象中找不到. 有没有办法将当前的URL输入getServersideprops中? 推荐答案您可以从上下文参数中使用resolvedUrl字段. export async function getServerSideProps({ req, res, resolvedUrl }) { console.log(resolvedUrl) // Remaining code }来自 getServerSideProps getServerSideProps 文档: resolvedUrl:请求URL的标准化版本 _next/data客户端过渡的前缀并包括原始查询值. 请注意,resolvedUrl不会返回URL的域部分,只返回路径和查询字符串. 其他推荐答案您可以在输入请求中通过标头对象访问域名 // localhost:3000 context.req.headers.host如果您想要路径的其余部分,则可以在"传入请求对象"中的URL属性中使用它. //blog/3 context.req.url完整示例: export function getServerSideProps (context){ // localhost:3000 const domain = context.req.headers.host // /blog/3 const path = context.req.url //localhost:3000/blob/3 const fullAddress = domain + path }本文地址:https://www.itbaoku.cn/post/2593400.html?view=all |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |