Element bindings

  1. bind:property={variable}
  1. bind:group={variable}
  1. bind:this={dom_node}

数据通常从父级流向子级。bind:指令允许数据以另一种方式从子级流向父级。大多数的绑定是绑定在特定的元素。

最简单反映了属性的值的绑定,例如input.value

  1. <input bind:value={name}>
  2. <textarea bind:value={text}></textarea>
  3. <input type="checkbox" bind:checked={yes}>

如果,属性值和属性名相同,也可以用简写的方式:

  1. <!-- These are equivalent -->
  2. <input bind:value={value}>
  3. <input bind:value>

强制输入的值是数字;即使input.value是一个字符串,svelte将把它当作一个数字。如果输入为空或无效(当type="number"),则该值为 undefined

  1. <input type="number" bind:value={num}>
  2. <input type="range" bind:value={num}>

Binding related elements

可以使用 bind:group 来让 input 一起工作

  1. <script>
  2. let tortilla = 'Plain';
  3. let fillings = [];
  4. </script>
  5. <!-- grouped radio inputs are mutually exclusive -->
  6. <input type="radio" bind:group={tortilla} value="Plain">
  7. <input type="radio" bind:group={tortilla} value="Whole wheat">
  8. <input type="radio" bind:group={tortilla} value="Spinach">
  9. <!-- grouped checkbox inputs populate an array -->
  10. <input type="checkbox" bind:group={fillings} value="Rice">
  11. <input type="checkbox" bind:group={fillings} value="Beans">
  12. <input type="checkbox" bind:group={fillings} value="Cheese">
  13. <input type="checkbox" bind:group={fillings} value="Guac (extra)">

Binding <select> value

<select>绑定的值,对应的是 selected 的<option>上的value属性的值,该值可以是任何值(不仅仅是字符串,可以是 DOM 中出现的值)。

  1. <select bind:value={selected}>
  2. <option value={a}>a</option>
  3. <option value={b}>b</option>
  4. <option value={c}>c</option>
  5. </select>

<select multiple> 的表现和 checkbox 组一样:

  1. <select multiple bind:value={fillings}>
  2. <option value="Rice">Rice</option>
  3. <option value="Beans">Beans</option>
  4. <option value="Cheese">Cheese</option>
  5. <option value="Guac (extra)">Guac (extra)</option>
  6. </select>

<option>value,和<option> 中的文本内容相同,则 value 属性可以被省略。

  1. <select multiple bind:value={fillings}>
  2. <option>Rice</option>
  3. <option>Beans</option>
  4. <option>Cheese</option>
  5. <option>Guac (extra)</option>
  6. </select>

Media element bindings

媒体元素(audiovideo)有自己的一套绑定规则:

四个只读的属性

  • duration(只读) - video 的总时长,单位是秒
  • buffered(只读) - 一个{start, end}对象数组
  • seekable(只读) - 同上
  • played(只读) - 同上

三个可以双向绑定的属性

  • currentTime: 当前视频的播放的位置,单位是秒
  • paused: 是否是暂停
  • volume: 音量,是01之间的值
  1. <video
  2. src={clip}
  3. bind:duration
  4. bind:buffered
  5. bind:seekable
  6. bind:played
  7. bind:currentTime
  8. bind:paused
  9. bind:volume
  10. ></video>

Block-level element bindings

块级元素有4个只读的绑定,其值的计算使用的技术方式和这个类似:

  • clientWidth
  • clientHeight
  • offsetWidth
  • offsetHeight
  1. <div
  2. bind:offsetWidth={width}
  3. bind:offsetHeight={height}
  4. >
  5. <Chart {width} {height}/>
  6. </div>

Binding a DOM node

要获取一个 DOM 节点的引用,可以使用bind:this

  1. <script>
  2. import { onMount } from 'svelte';
  3. let canvasElement;
  4. onMount(() => {
  5. const ctx = canvasElement.getContext('2d');
  6. drawStuff(ctx);
  7. });
  8. </script>
  9. <canvas bind:this={canvasElement}></canvas>