欢迎使用 Svelte 参考文档! 这是为那些已经对 Svelte 有一定了解并希望了解更多有关使用它的人提供的资源。
英Welcome to the Svelte reference documentation! This is intended as a resource for people who already have some familiarity with Svelte and want to learn more about using it.
如果你(还)不是这样,你可能更愿意在查阅本参考资料之前先访问 互动教程 或 示例。 你可以使用 REPL 在线尝试 Svelte。 或者,如果你想要一个功能更齐全的环境,你可以在 StackBlitz 上尝试 Svelte。
英If that's not you (yet), you may prefer to visit the interactive tutorial or the examples before consulting this reference. You can try Svelte online using the REPL. Alternatively, if you'd like a more fully-featured environment, you can try Svelte on StackBlitz.
开始一个新项目(Start a new project)permalink
我们推荐使用 SvelteKit,Svelte 团队的官方应用框架:
英We recommend using SvelteKit, the official application framework from the Svelte team:
npm create svelte@latest myapp
cd myapp
npm install
npm run dev
SvelteKit 将处理调用 Svelte 编译器 将 .svelte
文件转换为 .js
文件,创建 DOM 和 .css
文件来设置其样式。 它还提供构建 Web 应用所需的所有其他部分,例如开发服务器、路由、部署和 SSR 支持。 SvelteKit 使用 Vite 来构建你的代码。
英SvelteKit will handle calling the Svelte compiler to convert your .svelte
files into .js
files that create the DOM and .css
files that style it. It also provides all the other pieces you need to build a web application such as a development server, routing, deployment, and SSR support. SvelteKit uses Vite to build your code.
SvelteKit 的替代品(Alternatives to SvelteKit)permalink
如果你出于某种原因不想使用 SvelteKit,你也可以通过运行 npm create vite@latest
并选择 svelte
选项来将 Svelte 与 Vite 一起使用(但不使用 SvelteKit)。 这样,npm run build
将在 dist
目录中生成 HTML、JS 和 CSS 文件。 在大多数情况下,你可能还需要 选择路由库。
英If you don't want to use SvelteKit for some reason, you can also use Svelte with Vite (but without SvelteKit) by running npm create vite@latest
and selecting the svelte
option. With this, npm run build
will generate HTML, JS and CSS files inside the dist
directory. In most cases, you will probably need to choose a routing library as well.
或者,有 适用于所有主流 Web 打包器的插件 来处理 Svelte 编译 — 这将输出 .js
和 .css
,你可以将它们插入到 HTML 中 — 但大多数其他人不会处理 SSR。
英Alternatively, there are plugins for all the major web bundlers to handle Svelte compilation — which will output .js
and .css
that you can insert into your HTML — but most others won't handle SSR.
编辑器工具(Editor tooling)permalink
Svelte 团队维护着 VS Code 扩展,并且还与各种其他 编辑器 和工具集成。
英The Svelte team maintains a VS Code extension and there are integrations with various other editors and tools as well.
寻求帮助(Getting help)permalink
不要羞于在 Discord 聊天室 中寻求帮助! 你还可以在 Stack Overflow 上找到答案。
英Don't be shy about asking for help in the Discord chatroom! You can also find answers on Stack Overflow.