如何在Svelte 3中拥有条件属性? [英] How to have a conditional attribute in Svelte 3?

查看:43
本文介绍了如何在Svelte 3中拥有条件属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有更简单的方法来编写以下复选框组件:

<script>
  export let disabled = false;
</script>

{#if disabled}
  <label class="checkbox" disabled>
    <input type="checkbox" {disabled} />
    <slot></slot>
  </label>
{:else}
  <label class="checkbox">
    <input type="checkbox" {disabled} />
    <slot></slot>
  </label>
{/if}

拥有<label disabled="false">是不可接受的,因为Bulma有一个css类.checkbox[disabled]

推荐答案

disabled || null(或disabled || undefined)会起作用:

<label class="checkbox" disabled={disabled || null}>
  <input type="checkbox" {disabled} />
  <slot></slot>
</label>

来自docs

.[A]包括属性,除非它们的值为nullish (nullundefined)。

<input required={false} placeholder="This input field is not required">
<div title={null}>This div has no title attribute</div>

这篇关于如何在Svelte 3中拥有条件属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆