如何在routeLoader$()中访问URL参数?

huangapple go评论65阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年7月4日 20:42:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76612766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定