mefechoel/svelte-navigator

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

evrys opened this issue · 1 comments

evrys commented

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

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>