Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

how do i get access to my route parameter #648

Closed
shavyg2 opened this issue Apr 30, 2019 · 10 comments
Closed

how do i get access to my route parameter #648

shavyg2 opened this issue Apr 30, 2019 · 10 comments

Comments

@shavyg2
Copy link

shavyg2 commented Apr 30, 2019

I am using v3 and it complains if i do default export.
it says query and params doesn't exist so i can't just use them undeclared.
How do i get access to this.

@maxmilton
Copy link

Documentation for the latest Sapper updates is still a work in progress. You can follow along with it's progress in #638 .

To get access to the page store, which contains the objects you want, try:

<script>
  import { page } from '@sapper/app';

  $: console.log($page);
</script>

<p>Page URL queries:</p>
<pre>{JSON.stringify($page.query, null, 2)}</pre>

Also, the docs about how stores work is over at https://svelte.dev/docs#svelte_store

@maxmilton
Copy link

Also worth noting this might be changing with #642

@Conduitry
Copy link
Member

This has changed in 0.26.0 final, and is described here

@jcollum
Copy link

jcollum commented Aug 22, 2019

I wish the docs explained it better. I'm doing this:

  import { stores } from '@sapper/app';
  import HighlightedLink from '../components/HighlightedLink.svelte';

  const { preloading, page, session } = stores(); 
  const { host, path, params, query } = page;
  console.log('page values', host,  path, params, query);

All the values are undefined. I am trying to get to the value in the url after the segment value, like if I am on /data/abc I need the abc value. Is there a version of segment that is an array?

@imCorfitz
Copy link

@jcollum page is a subscription. Try to change this line:

// Add Dollar sign before page variable
const { host, path, params, query } = $page;

host may still be undefined.. I actually don't know why that is the case, but the rest should be available to you then.

@jcollum
Copy link

jcollum commented Sep 15, 2019

Thanks. I sorted it out and put up a gist: https://gist.github.com/jcollum/29211cf188c4174d75e2cf1a90b2cb68

Line 11 and 19 are the key lines there

@ghost
Copy link

ghost commented Mar 9, 2020

Following @jcollum gist, this worked for me:

<script>
  import { stores } from '@sapper/app';
  const { page } = stores();
  $: console.log($page.path);
</script>

Versions:
"sapper": "^0.27.0",
"svelte": "^3.0.0",

@jokkebk
Copy link

jokkebk commented Apr 24, 2021

It seems this is made a bit too complex, I wish there was a magic thingy that would make export let myparam work in a route [myparam].svelte. Alternative to previous:

<script context="module">
	export async function preload(page) {
		return {uid: page.params.myparam};
	}
</script>

<script>
	export let myparam;
</script>

@rotimi-best
Copy link

Given this folder structure playlist/id/video/id/index.svelte , how do I get the playlist id in index.svelte?

@imCorfitz
Copy link

Given this folder structure playlist/id/video/id/index.svelte , how do I get the playlist id in index.svelte?

In your case, you should make sure to use [ ] whenever you want to catch your params.

e.g. playlist/id/video/[id].svelte or playlist/id/video/[id]/index.svelte

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants