mefechoel / svelte-navigator

Simple, accessible routing for Svelte
Other
504 stars 39 forks source link

What's the svelte-navigator way to access query params? #53

Closed evrys closed 2 years ago

evrys commented 2 years ago

e.g. if I have a url like /login?next=/settings, what's the proper way to access the value of "next"?

I couldn't quite figure this out from the README

mefechoel commented 2 years ago

There is no built-in way of parsing query strings in svelte-navigator, as I feel that is very much up to personal preference and project structure, so you can use any means of parsing query strings you like. I'd recommend using the ES URLSearchParams standard. It is very well supported by modern browsers. You could use it like so:

<script>
  import { useLocation } from "svelte-navigator";

  const location = useLocation();

  // When you're at `/login?next=/settings`, `$location.search` will be `?next=/settings
  const searchParams = new URLSearchParams($location.search);
  const next = searchParams.get("next"); // -> "/settings" in your example
</script>