获取一个苗条的嵌套自定义商店的价值,在另一个自定义商店内

时间:2021-01-18 19:11:29

标签: svelte svelte-store

基于我制作的 this REPL example。我创建了两个这样的苗条自定义商店:

<script>
    import { writable } from "svelte/store";
    
    var store1;
    const {subscribe:subscribe_store1, set:set_store1, update:update_store1} = writable(store1)
    const Store1 = {
        subscribe:subscribe_store1,
        customfunction1:CustomFunction1
    }
    
    function CustomFunction1(str){set_store1(str)}
    
    
    
    var store2;
    const {subscribe:subscribe_store2, set:set_store2, update:update_store2} = writable(store1)
    const Store2 = {
        subscribe:subscribe_store2,
        customfunction2:CustomFunction2
    }
    
    function CustomFunction2(str){set_store2(str)}
    
    
    Store1.customfunction1("is 1!")
    Store2.customfunction2("is 2!")
</script>

<h1>Store1 : {$Store1}</h1>
<h2>Store2 : {$Store2}</h2>

如何使用 Store1 访问 $Store2 的值?我只找到了 this solution,但是必须一直导入和调用 get 函数有点烦

编辑:它不是反应性的...


import {writable, get} from "svelte/store"

...

const Store2 = {
   subscribe:subscribe_store2,
   Store1:Store1,
   customfunction2:CustomFunction2
}

...

<h3>{get({Store2.Store1)}</h3>

如果你有更好的方法,我会接受。

编辑:如果你有什么工作,我肯定会接受。

1 个答案:

答案 0 :(得分:0)

这是我发现的一个解决方案,假设 Store1 是反应性的

$: store1 = Store2.Store1

<h3>{$store1}</h3>