Skip to main content

readonly this 绑定适用于每个元素(和组件),并允许你获取对渲染元素的引用。 例如,我们可以获得对 <canvas> 元素的引用:

The readonly this binding applies to every element (and component) and allows you to obtain a reference to rendered elements. For example, we can get a reference to a <canvas> element:

<canvas bind:this={canvas} width={32} height={32} />

请注意,在组件安装之前,canvas 的值将是 undefined,因此我们将逻辑放在 onMount 生命周期函数 中。

Note that the value of canvas will be undefined until the component has mounted, so we put the logic inside the onMount lifecycle function.