【问题标题】:Svelte: Using reactive statement based on module context variableSvelte:使用基于模块上下文变量的反应式语句
【发布时间】:2020-03-28 16:27:44
【问题描述】:

我想使用跨组件的多个实例共享的代码来启动每个组件内的代码。

我尝试使用反应式语句来做到这一点:

<script context="module">
    let what = 0;
</script>

<script>
    export let number;
    $: if (what === number) [...]
</script>

但对 what 的更改不会触发该反应性语句的重新运行。

为什么这个 REPL 不起作用,我该如何解决?

https://svelte.dev/repl/38b94490982f4f3c80644fd364b50723?version=3.16.0

【问题讨论】:

    标签: svelte svelte-component


    【解决方案1】:

    what 更改为writable 似乎可以解决问题:

    <script context="module">
        import { writable } from 'svelte/store';
        const what = writable(0);
    </script>
    
    <script>
        export let number;
        $: if ($what === number) [...]
    </script>
    

    https://svelte.dev/repl/f667f3eb6b7d453da1473d5e26268814?version=3.16.0

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 2019-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-15
    相关资源
    最近更新 更多