我想在页面加载之前使用 url 的查询字符串来获取一些数据,但是当我打印出参数时,我只能得到路径。
使用它作为启动项目:
npx create-next-app test --use-npm --example "https://github.com/vercel/next-learn/tree/master/basics/basics-final"
转到pages/posts/[id].js
并添加console.log(params)
到该getStaticProps
功能。
然后导航到http://localhost:3000/posts/pre-rendering?abc=123
。服务器记录{ id: 'pre-rendering' }
而不是{ id: 'pre-rendering', abc: '123' }
我在非动态路由页面上尝试了相同的方法。转到,http://localhost:3000/?a=b
服务器日志将显示undefined
我不想使用 useRouter 因为这似乎只在页面加载后显示查询字符串。第一个开发控制台日志是undefined
,然后只记录查询字符串。
如何在页面加载之前访问查询字符串?