Skip to main content

什么是符文?

符文 /ro͞on/ 名词

¥[!NOTE] rune /ro͞on/ noun

用作神秘或魔法符号的字母或标记。

¥A letter or mark used as a mystical or magic symbol.

符文是你在 .svelte.svelte.js / .svelte.ts 文件中用来控制 Svelte 编译器的符号。如果你将 Svelte 视为一种语言,符文是语法的一部分 - 它们是关键字。

¥Runes are symbols that you use in .svelte and .svelte.js / .svelte.ts files to control the Svelte compiler. If you think of Svelte as a language, runes are part of the syntax — they are keywords.

符文具有 $ 前缀并且看起来像函数:

¥Runes have a $ prefix and look like functions:

let let message: stringmessage = 
function $state<"hello">(initial: "hello"): "hello" (+1 overload)
namespace $state

Declares reactive state.

Example:

let count = $state(0);

https://svelte.dev/docs/svelte/$state

@paraminitial The initial value
$state
('hello');

但是,它们在重要方面与普通 JavaScript 函数不同:

¥They differ from normal JavaScript functions in important ways, however:

  • 你不需要导入它们 - 它们是语言的一部分

    ¥You don’t need to import them — they are part of the language

  • 它们不是值 — 你不能将它们分配给变量或将它们作为参数传递给函数

    ¥They’re not values — you can’t assign them to a variable or pass them as arguments to a function

  • 就像 JavaScript 关键字一样,它们仅在某些位置有效(如果你将它们放在错误的位置,编译器会帮助你)

    ¥Just like JavaScript keywords, they are only valid in certain positions (the compiler will help you if you put them in the wrong place)

Legacy mode

在 Svelte 5 之前,符文并不存在。

¥[!LEGACY] Runes didn’t exist prior to Svelte 5.