英文:
Qwik - How to access URL parameters in routeLoader$()?
问题
我想使用useLocation()在routeLoader$()中访问URL参数:
export const useRouteLoader = routeLoader$(
(requestEvent: RequestEvent) => {
const loc = useLocation();
console.log(loc.params);
//其他代码
}
);
但它不起作用。我想将URL参数记录到控制台。我该怎么做?
英文:
I want to access the URL parameters with useLocation() in a routeLoader$():
export const useRouteLoader = routeLoader$(
(requestEvent: RequestEvent) => {
const loc = useLocation();
console.log(loc.params);
//Other code
}
);
But it does not work. I want to log the URL params to the console. How do i do that?
答案1
得分: 1
你不能在 routeLoader$() 中使用 useLocation(),但 routeLoader$ 具有访问 RequestEvent API 的权限,其中包括有关当前HTTP请求的信息,包括URL参数。
export const useRouteLoader = routeLoader$((requestEvent) => {
console.log(requestEvent.params);
// 其他代码
});
在 routeLoader$() Qwik City documentation 中查看更多关于 routeLoader$() 的信息。
在 RequestEvent Qwik City documentation 中查看RequestEvent包含的内容。
英文:
You cannot use useLocation() in routeLoader$()s, but routeLoader$ has access to the RequestEvent API which includes information about the current HTTP request, including the URL params.
export const useRouteLoader = routeLoader$((requestEvent) => {
console.log(requestEvent.params);
//Other code
});
See more about routeLoader$() at routeLoader$() Qwik City documentation.
See what the RequestEvent contains at RequestEvent Qwik City documentation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论