如何访问导入组件中设置的 sveltekit 中的变量?
How do you access a variable in sveltekit set in an imported compontent?
我知道如何在 sveltekit 中使用 props,但是如何同时以另一种方式在 Nested.svelte 中获取变量“高度”?
//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>
您需要在组件上声明一个局部变量 use bind
:
<script>
import Nested from './Nested.svelte';
let height;
</script>
<Nested bind:height />
或者没有shorthand如果变量名不同:
<script>
import Nested from './Nested.svelte';
let nestedHeight;
</script>
<Nested bind:height={nestedHeight} />
我知道如何在 sveltekit 中使用 props,但是如何同时以另一种方式在 Nested.svelte 中获取变量“高度”?
//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>
您需要在组件上声明一个局部变量 use bind
:
<script>
import Nested from './Nested.svelte';
let height;
</script>
<Nested bind:height />
或者没有shorthand如果变量名不同:
<script>
import Nested from './Nested.svelte';
let nestedHeight;
</script>
<Nested bind:height={nestedHeight} />