【发布时间】:2020-11-10 19:30:49
【问题描述】:
在 Svelte 应用程序中,我有以下国家/地区数组:
let countries = [
{
name:"Alegeria",
status: "1"
},
{
name:"Bulgaria",
status :"0"
}
]
注意status 属性是一个字符串。我以这种方式迭代数组:
{#if countries.length > 0}
<table class="table">
<thead>
<tr>
<th>Country</th>
<th class="text-right">Status</th>
</tr>
</thead>
<tbody>
{#each countries as c}
<tr>
<td>{c.name}</td>
<td class="text-right"><Switch bind:checked={Boolean(Number(c.status))} /></td>
</tr>
{/each}
</tbody>
</table>
{:else}
<p class="alert alert-danger">No countries found</p>
{/if}
如您所见,我尝试使用Boolean(Number(c.status)) 将status 属性的值转换为布尔值。
我得到了错误,而不是所需的转换:Can only bind to an identifier (e.g. foo) or a member expression,如 REPL 所示。
我做错了什么?
【问题讨论】:
-
为什么不直接绑定国家状态?它必须是 0 和 1 而不是 false 和 true 吗?
-
@Gh05d 我仍然需要将字符串
"0"和"1"转换为字符串0和1的数字。所以我会有同样的问题。
标签: javascript svelte svelte-3