Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do you access a variable in sveltekit set in an imported compontent?

I know how to use props in sveltekit but how do you get a variable "height" the other way set in Nested.svelte at the same time?

//App.svelte
<script>
    import Nested from './Nested.svelte';
</script>
{height}
<Nested answer={42}/>

//Nested.svelte
<script>
    export let answer;
    export let height;
</script>

<div bind:clientHeight={height}>The answer is {answer}</div>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You need to declare a local variable use bind on the component:

<script>
    import Nested from './Nested.svelte';
    let height;
</script>
<Nested bind:height />

Or without shorthand if the variable name is different:

<script>
    import Nested from './Nested.svelte';
    let nestedHeight;
</script>
<Nested bind:height={nestedHeight} />

REPL

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading