我无法访问astro文件的查询参数。
给定url http://localhost:3000/example?hello=meow
如何从{hello: "meow"}
文件中访问example.astro
?
发布于 2022-08-18 15:10:14
如果要编写API路由,则需要使用.ts
或.js
文件[来源]。
在这种情况下,您可以编写如下所示的get
方法:
export async function get({request}) {
// process the URL into a more usable format
const url = new URL(request.url)
const params = new URLSearchParams(url.search)
// set up a response object
const data = {
hello: params.get('hello'),
};
// this will yield { hello: 'meow' } on your Astro server console
console.log(data)
// return the response
return new Response(JSON.stringify(data), {
status: 200
}); }
我认为,从一个.astro
文件中,您可以通过const { url } = Astro.request;
来访问url。对于静态页面,可以通过const url = Astro.url;
[来源]完成。
https://stackoverflow.com/questions/73382984
复制相似问题