svelte-template-directives

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Svelte Template Directives

Svelte模板指令

@attach (Svelte 5.29+)

@attach(Svelte 5.29+)

The reactive alternative to
use:
actions.
Re-runs when dependencies change, passes through components via spread, supports cleanup functions.
svelte
<script>
	import ImageZoom from 'js-image-zoom';

	function useZoom(options) {
		return (element) => {
			new ImageZoom(element, options);
			return () => console.log('cleanup');
		};
	}
</script>

<!-- Re-runs if options changes (use: wouldn't!) -->
<figure {@attach useZoom({ width: 400 })}>
	<img src="photo.jpg" alt="zoomable" />
</figure>
use:
动作的响应式替代方案
。当依赖项变化时重新运行,通过展开语法传递到组件,支持清理函数。
svelte
<script>
\timport ImageZoom from 'js-image-zoom';

\tfunction useZoom(options) {
\t\treturn (element) => {
\t\t\tnew ImageZoom(element, options);
\t\t\treturn () => console.log('cleanup');
\t\t};
\t}
</script>

<!-- 当options变化时重新运行(use:不会!) -->
<figure {@attach useZoom({ width: 400 })}>
\t<img src="photo.jpg" alt="zoomable" />
</figure>

Quick Reference

快速参考

DirectivePurposeReactive?
{@attach}
DOM manipulation, 3rd-partyYes
{@html}
Render raw HTML stringsYes
{@render}
Render snippetsYes
{@const}
Local constants in blocksN/A
{@debug}
Pause debugger on value changeN/A
{#each (key)}
Keyed iteration (always key!)Yes
<svelte:window>
Window event listenersN/A
指令用途响应式?
{@attach}
DOM操作、第三方库
{@html}
渲染原始HTML字符串
{@render}
渲染代码片段
{@const}
代码块中的本地常量不适用
{@debug}
值变化时暂停调试器不适用
{#each (key)}
带键的迭代(务必使用键!)
<svelte:window>
窗口事件监听器不适用

@attach vs use: Actions

@attach vs use: 动作

Feature
use:
@attach
Re-runs on arg changeNoYes
ComposableLimitedFully
Pass through propsManualAuto via spread
Convert legacyN/A
fromAction()
特性
use:
@attach
参数变化时重新运行
可组合性有限完全支持
传递props手动通过展开自动传递
转换旧版动作不适用
fromAction()

Reference Files

参考文件

  • attach-patterns.md - Real-world @attach examples
  • other-directives.md - @html, @render, @const, @debug
  • attach-patterns.md - @attach的真实场景示例
  • other-directives.md - @html、@render、@const、@debug相关内容

Notes

注意事项

  • @attach
    requires Svelte 5.29+
  • Use
    fromAction
    from
    svelte/attachments
    to convert legacy actions
  • Attachments pass through wrapper components when you spread props
  • Always use keyed each blocks — never use index as key
  • Use
    <svelte:window>
    /
    <svelte:document>
    for global events, not
    $effect
  • Last verified: 2026-03-12
<!-- PROGRESSIVE DISCLOSURE GUIDELINES: - Keep this file ~50 lines total (max ~150 lines) - Use 1-2 code blocks only (recommend 1) - Keep description <200 chars for Level 1 efficiency - Move detailed docs to references/ for Level 3 loading - This is Level 2 - quick reference ONLY, not a manual -->
  • @attach
    需要Svelte 5.29+版本
  • 使用
    svelte/attachments
    中的
    fromAction()
    转换旧版动作
  • 当你展开props时,附件会传递到包装组件中
  • 务必使用带键的each代码块——永远不要用索引作为键
  • 使用
    <svelte:window>
    /
    <svelte:document>
    处理全局事件,而非
    $effect
  • **最后验证时间:**2026-03-12
<!-- 渐进式披露指南: - 保持此文件约50行(最多约150行) - 仅使用1-2个代码块(推荐1个) - 描述部分保持在200字符以内,以实现一级效率 - 将详细文档移至references/目录,用于三级加载 - 此为二级内容——仅快速参考,非手册 -->",