bind - 在 Svelte 中,当复选框位于组件中时如何绑定(bind) :group work,?

标签 bind svelte svelte-component

当复选框不在组件中时,我将 bind:group 用于复选框。 现在,当我尝试将带有标签的复选框作为组件时,它不起作用。

CheckboxWithLabel.svelte(组件)

<script>
    export let label="";
    export let bindGroup="";
    export let value="";
</script>
<label class="container">{label}
    <input type="checkbox" bind:group={bindGroup} value={value} />
    <span class="checkmark"></span>
</label>

SettingsSession.svelte(页面)

import CheckboxWithLabel from '@/components/ui/CheckboxWithLabel';

<script>
let sessionLengths = [5, 15];
$: console.log('duration', sessionLengths);
</script>

<div class="form-group">
    <h5>Select live session durations</h5>
    <CheckboxWithLabel label='5 minutes' bindGroup={sessionLengths} value="5"/>
    <CheckboxWithLabel label='15 minutes' bindGroup={sessionLengths} value="15"/>
    <CheckboxWithLabel label='30 minutes' bindGroup={sessionLengths} value="30"/>
    <CheckboxWithLabel label='45 minutes' bindGroup={sessionLengths} value="45"/>
    <CheckboxWithLabel label='60 minutes' bindGroup={sessionLengths} value="60"/>
    <CheckboxWithLabel label='90 minutes' bindGroup={sessionLengths} value="90"/>
    <CheckboxWithLabel label='120 minutes' bindGroup={sessionLengths} value="120"/>
</div>
...

在没有组件的情况下使用 bind:group 的简单示例。

<script>
let goodDogs = []
let dogs = ['Roger', 'Syd']
</script>

<h2>
  Who's a good dog?
</h2>

<ul>
  {#each dogs as dog}
    <li>{dog} <input type=checkbox bind:group={goodDogs} value={dog}></li>
  {/each}
</ul>

<h2>
  Good dogs according to me:
</h2>

<ul>
  {#each goodDogs as dog}
    <li>{dog}</li>
  {/each}
</ul>

来源:https://www.freecodecamp.org/news/the-svelte-handbook/#svelte-lifecycle-events

最佳答案

实际上,我在 Svelte Github 存储库上搜索了问题。这是报道issue .

关于bind - 在 Svelte 中,当复选框位于组件中时如何绑定(bind) :group work,?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61911455/

相关文章:

javascript - jQuery:尝试绑定(bind)事件时对对象的错误引用

sockets - Debian - 每个用户的默认 IP?

SvelteKit:如何使用 www-authenticate header 保护页面

svelte - 自动将 html 属性传递给 svelte 组件

javascript - 在 typescript svelte 中导入别名 svelte 组件

nginx - 重启 nginx : nginxnginx: [emerg] bind() to 0. 0.0.0:80 failed (98: Address already in use)

javascript - 有没有办法删除 jQuery 中的所有命名空间绑定(bind)?

visual-studio-code - Visual Studio Code 无法识别 Svelte 中的 SCSS

svelte - 如何从 DOM 结构中删除随机的精简预处理类?

SvelteJS : Passing data to components