简体   繁体   English

Svelte:组件上下文

[英]Svelte: Component context

I created a custom dropdown component.我创建了一个自定义下拉组件。 I have multiples instances of it on the same page like this:我在同一页面上有多个实例,如下所示:

label : <Select>
label : <Select>
label : <Select>

Whenever I click on the component, the first one opens only.每当我点击组件时,第一个只会打开。 How can I fix this?我怎样才能解决这个问题? Thank you.谢谢你。

// Select.svelte // Select.svelte

<div class="dropdown">
<slot></slot>
</div>

SelectItem.svelte SelectItem.svelte

<Select>
 content
</Select>

items.svelte items.svelte

{#each items as item}
<item />
{/each}

item.svelte item.svelte

<SelectItem/>

Ideally, you would have something like this理想情况下,你会有这样的东西

<script>
//App.svelte
    import Select from "./Select.svelte"
    let options = [
        {label:"State 1", id:1},
        {label:"State 2", id:2}
    ]
    let country = [
        {label:"Country 1", id:1},
        {label:"Country 2", id:2}
    ]
</script>


<Select options={options}/>

<Select options={country} />
<script>
//Select.svelte
    export let options = []
</script>


<select>
        {#each options as option} 
            <option>{option.label}</option>
        {/each}
</select>

Check REPL检查REPL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM