Svelte:如何阻止每次绑定属性更改时刷新 {#await} 块?

Ash*_*nni 4 svelte svelte-component

我正在尝试使用<select/>从基于 Promise 的函数获得的数据来初始化输入。输入初始化选项后(每个选项从解析的数据中获取值和标签),属性将绑定到<select/>.

但是每次我更改选项(使用属性绑定)时,{#await}块内的所有内容都会重新加载(似乎它解决了相同的 Promise 并重置了选项)。

当我删除绑定时,不会发生这种情况。

我已经尝试过以下方法:

这是当前状态的片段:

等待块:

<div class="device-select container">
  {#await VoiceStreamingService.get_microhpones()}

  {:then devices}
    <select id="device-options">
      <option selected disabled>Select an Option...</option>
      {#each devices as device (device.deviceId)}
        <option value={device.deviceId}>{device.label}</option>
      {/each}
    </select>
  {:catch}

  {/await}
  <button on:click={set_selected_device}>Connect To</button>
</div>
Run Code Online (Sandbox Code Playgroud)

set_selected_device函数:

function set_selected_device() {
    let d = document.getElementById("device-options");
    selected_device = d.options[d.selectedIndex].value;
    console.log(selected_device);
  }
Run Code Online (Sandbox Code Playgroud)

我是否遗漏了一些重要的东西,或者这是一个错误?

Ric*_*ris 6

恐怕这是一个错误:https://github.com/sveltejs/svelte/issues/2355

解决方法是在脚本中创建一个变量...

let promise = VoiceStreamingService.get_microhpones();
Run Code Online (Sandbox Code Playgroud)

并等待它而不是表达式。