Vue3 Composition API: 同一组件的多个实例共享状态

huangapple go评论64阅读模式
英文:

Vue3 Composition API: Multiple instances of same Component share state

问题

I'm working my way through Vue 3 and the Composition API and I can't figure out how to make multiple instances of the same component work.

<Loader />
<Loader />
<Loader />

Loader.vue

export default {
    data() {
        return {
            status: null
        }
    }
}

Now, when the first Loader ist done and sets status, the other <Loader /> components work as expected.

However, when using Composition API like follows, the first <Loader /> that is done sets the status und all the other Components use that new status instead working with there own variables.

const status = ref(null)

How can I achieve this behaviour in Vue3 with Composition API?

I found an old question about a similar problem but this doesn't help with Composition API. I guess, it's related to state management of the components. Also, the official docs don't help.

英文:

I'm working my way through Vue 3 and the Composition API and I can't figure out how to make multiple instances of the same component work.

&lt;Loader /&gt;
&lt;Loader /&gt;
&lt;Loader /&gt;

Loader.vue

export default {
    data() {
        return {
            status: null
        }
    }
}

Now, when the first Loader ist done and sets status, the other &lt;Loader /&gt; components work as expected.

However, when using Composition API like follows, the first &lt;Loader /&gt; that is done sets the status und all the other Components use that new status instead working with there own variables.

const status = ref(null)

How can I achieve this behaviour in Vue3 with Composition API?

I found an old question about a similar problem but this doesn't help with Composition API. I guess, it's related to state management of the components. Also, the official docs don't help.

答案1

得分: 1

以下是翻译好的部分:

所以我不明白你是如何将状态传递给 Loader 的。你应该在每个 Loader 上有一个像这样的属性。

<Loader :status="status"/>
<Loader :status="status"/>
<Loader :status="status"/>

然后在 Loader 中,你将不得不声明 props。

const props = defineProps(['status']);

现在,当你在父级中更新状态时,它应该传递到所有实例中。

如果这不是你遇到的问题,另一种类似的问题通常会出现,当你有多个具有相同名称的 div 时,缺少唯一的 ID。要解决这个问题,你可以像这样做:

<Loader :status="status" key="1"/>
<Loader :status="status" key="2"/>
<Loader :status="status" key="3"/>

这通常是在 foreach 循环中出现的问题,但在这种情况下,这也可能是你遇到的问题之一。

英文:

So I'm not seeing how you are passing the status into the Loader. You should have a property on each of the loaders like this.

&lt;Loader :status=&quot;status&quot;/&gt;
&lt;Loader :status=&quot;status&quot;/&gt;
&lt;Loader :status=&quot;status&quot;/&gt;

And then in Loader you'll have to declare the props

const props = defineProps([&#39;status&#39;]);

Now when you update the status in the parent it should carry through to all the instances.

If that isn't the issue you are having, another kind of similar issue that often will come up with you have several divs with the same name is the lack of a unique ID. To fix that you'll want to do something like this:

&lt;Loader :status=&quot;status&quot; key=&quot;1&quot;/&gt;
&lt;Loader :status=&quot;status&quot; key=&quot;2&quot;/&gt;
&lt;Loader :status=&quot;status&quot; key=&quot;3&quot;/&gt;

That is normally an issue with foreach loops, but it could be an issue that you are having as well in this case.

huangapple
  • 本文由 发表于 2023年6月5日 00:19:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401350.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定