gpt4 book ai didi

how can I access/count the children in a svelte slot?(我如何才能访问/清点一个超薄机位中的孩子?)

转载 作者:bug小助手 更新时间:2023-10-25 15:27:13 24 4
gpt4 key购买 nike



I need to know how many children are in a <slot /> is this possible? For example:

我想知道一个<槽/>里有多少个孩子,这可能吗?例如:


// Parent.svelte
<script>
let count = 0 // how to get something like slot.children.count??
</script>
<slot />

// Child.svelte
<div>child</div>

Usage:

用途:


<Parent> // count inside parent should be set to 3
<Child />
<Child />
<Child />
</Parent>

Not sure if I"m missing something obvious but I can't find this capability anywhere.

我不确定我是否遗漏了什么明显的东西,但我在任何地方都找不到这种功能。


After trying many different approaches without success, I have the rest of the functionality that needs to know the count working by just passing the count, but it really is not ideal:

在尝试了许多不同的方法都没有成功之后,我只需传递计数就可以知道计数的其余功能,但这真的不是很理想:


// Parent.svelte
<script>
export let count
</script>
<slot />

And use like:

并使用如下方式:


<Parent count={3}>
<Child />
<Child />
<Child />
</Parent>

But it would be much simpler to be able to calculate the child count to avoid mismatching between the amount of children and the count

但是,能够计算子计数以避免子数和计数之间的不匹配会简单得多


更多回答
优秀答案推荐

If the components can be coupled without causing issues elsewhere, you can define a context in the parent, get it in the child and make it "register" itself there.

如果组件可以耦合在一起而不会在其他地方引起问题,那么您可以在父级中定义上下文,将其放在子级中,并让它在那里“注册”自己。


Something along the lines of:

大概是这样的事情:


// in parent
// Use a store for reactivity
const count = setContext('count', writable(0));

<span>Child count: {$count}</span>

// in child
getContext('count').update(i => i + 1)

You could also query the DOM, though I would not recommend that.

您也可以查询DOM,尽管我不建议这样做。


There currently is no programmatic access to slot contents, except checking whether it was set at all.

目前没有对槽内容的编程访问,除了检查它是否被设置。


更多回答

That is what I'm doing now... I am able to count them no problem. Now I need to run a function with some data from each child but only run it with all the data not for each child. I think I've almost got it working

这就是我现在正在做的..。我能数出来,没问题。现在,我需要运行一个函数,其中包含来自每个子对象的一些数据,但只运行包含所有数据的函数,而不是每个子对象。我想我快把它修好了

You should ask questions about your actual goal, not some small part of what you think you need to do. Maybe your whole approach is not suitable and something else should be done entirely.

你应该问关于你实际目标的问题,而不是你认为你需要做的事情的一小部分。也许你的整个方法都不合适,应该完全做一些其他的事情。

Exactly, this reminds me to a possible XY-Problem

没错,这让我想起了一个可能的XY问题

This was my actual goal... But once I got that working and started integrating it, I realized I also needed to be able to have the parent function run with all children as inputs, for a parent like <Sum />, ie: <Sum><Number num={1}/><Number num={1}/><Number num={1}/></Sum> stackoverflow.com/questions/77062764/…

这是我的实际目标..。但一旦我开始工作并开始集成它,我意识到我还需要能够让父函数以所有子对象作为输入来运行,对于像这样的父对象,即:stackoverflow.com/Questions/77062764/…

Either update the question, or create a new one which includes the real problem or something that is actually representative of it. Would recommend the latter, shifting goal posts are not great and more people will probably see the new question. In any case, the comments are not the place for this.

要么更新问题,要么创建一个包含真正问题或实际代表问题的新问题。我会推荐后者,改变目标位置并不是很好,更多的人可能会看到新的问题。在任何情况下,评论都不是这个地方。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com