From 5d81f469bf1aa0b288b2e5768601db928ece554a Mon Sep 17 00:00:00 2001 From: cdcd72 Date: Tue, 27 Apr 2021 00:26:03 +0800 Subject: 初步翻譯 Getting started with Svelte 開頭及結尾說明 & 將 en-us 位址均導向為 zh-tw MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../01-svelte-starter-app.png | Bin 0 -> 13632 bytes .../02-svelte-component-scoped-styles.png | Bin 0 -> 34041 bytes .../03-svelte-repl-in-action.png | Bin 0 -> 39138 bytes .../svelte_getting_started/index.html | 554 +++++++++++++++++++++ 4 files changed, 554 insertions(+) create mode 100644 files/zh-tw/learn/tools_and_testing/client-side_javascript_frameworks/svelte_getting_started/01-svelte-starter-app.png create mode 100644 files/zh-tw/learn/tools_and_testing/client-side_javascript_frameworks/svelte_getting_started/02-svelte-component-scoped-styles.png create mode 100644 files/zh-tw/learn/tools_and_testing/client-side_javascript_frameworks/svelte_getting_started/03-svelte-repl-in-action.png create mode 100644 files/zh-tw/learn/tools_and_testing/client-side_javascript_frameworks/svelte_getting_started/index.html (limited to 'files/zh-tw/learn') diff --git a/files/zh-tw/learn/tools_and_testing/client-side_javascript_frameworks/svelte_getting_started/01-svelte-starter-app.png b/files/zh-tw/learn/tools_and_testing/client-side_javascript_frameworks/svelte_getting_started/01-svelte-starter-app.png new file mode 100644 index 0000000000..5a62c9589e Binary files /dev/null and b/files/zh-tw/learn/tools_and_testing/client-side_javascript_frameworks/svelte_getting_started/01-svelte-starter-app.png differ diff --git a/files/zh-tw/learn/tools_and_testing/client-side_javascript_frameworks/svelte_getting_started/02-svelte-component-scoped-styles.png b/files/zh-tw/learn/tools_and_testing/client-side_javascript_frameworks/svelte_getting_started/02-svelte-component-scoped-styles.png new file mode 100644 index 0000000000..5b002f996d Binary files /dev/null and b/files/zh-tw/learn/tools_and_testing/client-side_javascript_frameworks/svelte_getting_started/02-svelte-component-scoped-styles.png differ diff --git a/files/zh-tw/learn/tools_and_testing/client-side_javascript_frameworks/svelte_getting_started/03-svelte-repl-in-action.png b/files/zh-tw/learn/tools_and_testing/client-side_javascript_frameworks/svelte_getting_started/03-svelte-repl-in-action.png new file mode 100644 index 0000000000..f1f272a089 Binary files /dev/null and b/files/zh-tw/learn/tools_and_testing/client-side_javascript_frameworks/svelte_getting_started/03-svelte-repl-in-action.png differ diff --git a/files/zh-tw/learn/tools_and_testing/client-side_javascript_frameworks/svelte_getting_started/index.html b/files/zh-tw/learn/tools_and_testing/client-side_javascript_frameworks/svelte_getting_started/index.html new file mode 100644 index 0000000000..86bd9d51d3 --- /dev/null +++ b/files/zh-tw/learn/tools_and_testing/client-side_javascript_frameworks/svelte_getting_started/index.html @@ -0,0 +1,554 @@ +--- +title: Svelte 入門 +slug: >- + Learn/Tools_and_testing/Client-side_JavaScript_frameworks/Svelte_getting_started +tags: + - Beginner + - Frameworks + - JavaScript + - Learn + - Svelte + - client-side + - 初學者 + - 框架 + - 學習 + - 客戶端 +--- +
{{LearnSidebar}}
+{{PreviousMenuNext("Learn/Tools_and_testing/Client-side_JavaScript_frameworks/Vue_resources","Learn/Tools_and_testing/Client-side_JavaScript_frameworks/Svelte_todo_list_beginning", "Learn/Tools_and_testing/Client-side_JavaScript_frameworks")}}
+ +

此篇文章我們將提供摘要來說明 Svelte 框架。我們將會看到 Svelte 如何運作及它本身和其餘框架和工具之間的區別。接著我們將學習如何設置我們的開發環境並建立一個範例應用程式,了解其專案結構及如何在本地運行,最後可以將其建置於正式環境。

+ + + + + + + + + + + + +
預備知識: +

學習它,推薦你至少需熟悉基本的 HTMLCSSJavaScript 等程式語言且具備 終端機/命令列環境 基本知識。

+ +

Svelte 為一個編譯器,從我們的來源去產生出最小且高度優化的 JavaScript 程式碼;你將會需要 node 和 npm 來安裝此編譯器來建置你的應用程式。

+
學習目標:設置 Svelte 本地開發環境,建立初始應用程式,瞭解 Svelte 基本運作方式。
+ +

Svelte: A new approach to building rich user interfaces

+ +

Svelte provides a different approach to building web apps than some of the other frameworks covered in this module. While frameworks like React and Vue do the bulk of their work in the user's browser while the app is running, Svelte shifts that work into a compile step that happens only when you build your app, producing highly-optimized vanilla JavaScript.

+ +

The outcome of this approach is not only smaller application bundles and better performance, but also a developer experience that is more approachable for people that have limited experience of the modern tooling ecosystem.

+ +

Svelte sticks closely to the classic web development model of HTML, CSS, and JS, just adding a few extensions to HTML and JavaScript. It arguably has fewer concepts and tools to learn than some of the other framework options.

+ +

It's main current disadvantages are that it is a young framework — its ecosystem is therefore more limited in terms of tooling, support, plugins, clear usage patterns, etc. than more mature frameworks, and there are also less job opportunities. But it's advantages should be enough to make you interested to explore it.

+ +
+

Note: recently Svelte has added official TypeScript support, one of its most requested features. We'll look at it later on in this tutorial series.

+
+ +

We encourage you to go through the Svelte tutorial for a really quick introduction to the basic concepts, before returning to this tutorial series to learn how to build something slightly more in-depth. It should take you about 30 minutes to complete.

+ +

Use cases

+ +

Svelte can be used to develop small pieces of an interface or whole applications. You can either start from scratch letting Svelte drive your UI or you can incrementally integrate it into an existing application.

+ +

Nevertheless, Svelte is particularly appropriate to tackle the following situations:

+ + + +

Moreover, with the help of Sapper (a framework based on Svelte), you can also develop applications with advanced features like server-side rendering, code splitting, file-based routing and offline support. And then there's also Svelte Native, which lets you build native mobile applications.

+ +

How does Svelte work?

+ +

Being a compiler, Svelte can extend HTML, CSS, and JavaScript, generating optimal JavaScript code without any runtime overhead. To achieve this, Svelte extends vanilla web technologies in the following ways:

+ + + +

The compiler only intervenes in very specific situations and only in the context of Svelte components. Extensions to the JavaScript language are minimal and carefully picked in order to not break JavaScript syntax nor alienate developers. In fact, you will be mostly working with vanilla JavaScript.

+ +

First steps with Svelte

+ +

Being a compiler, you can't just add a <script src="svelte.js"> tag to your page and import it into your app. You'll have to set up your development environment in order to let the compiler do its job.

+ +

Requirements

+ +

In order to work with Svelte you need to have Node.js installed. It's recommended that you use the long-term support (LTS) version. Node includes npm (the node package manager), and npx (the node package runner). Note that you can also use the Yarn package manager in place of npm, but we'll assume you are using npm in this set of tutorials. See Package management basics for more information on npm and yarn.

+ +

If you're using Windows, you will need to install some software to give you parity with Unix/macOS terminal in order to use the terminal commands mentioned in this tutorial. Gitbash (which comes as part of the git for Windows toolset) or Windows Subsystem for Linux (WSL) are both suitable. Cmder is another very good and complete alternative. See Command line crash course for more information on these, and on terminal commands in general.

+ +

Also see the following for more information:

+ + + +

Creating your first Svelte app

+ +

The easiest way to create a starter app template is to just download the starter template application. You can do that by visiting sveltejs/template on GitHub or you can avoid having to download and unzip it and just use degit.

+ +

To create your starter app template, run the following terminal commands:

+ +
npx degit sveltejs/template moz-todo-svelte
+cd moz-todo-svelte
+npm install
+npm run dev
+ +
+

Note: degit doesn't do any kind of magic — it just lets you download and unzip the latest version of a git repo's contents. This is much quicker than using git clone because it will not download all the history of the repo, or create a complete local clone.

+
+ +

After running npm run dev, Svelte will compile and build your application. It will start a local server at localhost:5000. Svelte will watch for file updates, and automatically recompile and refresh the app for you when changes are made to the source files. Your browser will display something like this:

+ +

A simple start page that says hello world, and gives a link to the official svelte tutorials

+ +

Application structure

+ +

The starter template comes with the following structure:

+ +
moz-todo-svelte
+├── readme.md
+├── package.json
+├── package-lock.json
+├── rollup.config.js
+├── .gitignore
+├── node_modules
+├── public
+│   ├── favicon.ico
+│   ├── index.html
+│   ├── global.css
+│   └── build
+│       ├── bundle.css
+│       ├── bundle.css.map
+│       ├── bundle.js
+│       └── bundle.js.map
+└── src
+    ├── App.svelte
+    └── main.js
+ +

The contents are as follows:

+ + + +

Having a look at our first Svelte component

+ +

Components are the building blocks of Svelte applications. They are written into .svelte files using a superset of HTML.

+ +

All three sections — <script>, <styles>, and markup — are optional, and can appear in any order you like.

+ +
<script>
+  // logic goes here
+</script>
+
+<style>
+  /* styles go here */
+</style>
+
+<!-- markup (zero or more HTML elements) goes here -->
+ +
+

Note: For more information on the component format, have a look at the svelte documentation.

+
+ +

With this in mind, let's have a look at the src/App.svelte file that came with the starter template. You should see something like the following:

+ +
<script>
+  export let name;
+</script>
+
+<main>
+  <h1>Hello {name}!</h1>
+  <p>Visit the <a href="https://svelte.dev/tutorial">Svelte tutorial</a> to learn how to build Svelte apps.</p>
+</main>
+
+<style>
+  main {
+    text-align: center;
+    padding: 1em;
+    max-width: 240px;
+    margin: 0 auto;
+  }
+
+  h1 {
+    color: #ff3e00;
+    text-transform: uppercase;
+    font-size: 4em;
+    font-weight: 100;
+  }
+
+  @media (min-width: 640px) {
+    main {
+      max-width: none;
+    }
+  }
+</style>
+ +

The <script> section

+ +

The <script> block contains JavaScript that runs when a component instance is created. Variables declared (or imported) at the top level are 'visible' from the component's markup. Top-level variables is the way Svelte handles the component state, and they are reactive by default. We will explain in detail what this means later on.

+ +
<script>
+  export let name;
+</script>
+ +

Svelte uses the export keyword to mark a variable declaration as a property (or prop), which means it becomes accessible to consumers of the component (e.g. other components). This is one example of Svelte extending JavaScript syntax to make it more useful, while keeping it familiar.

+ +

The markup section

+ +

In the markup section you can insert any HTML you like, and in addition you can insert valid JavaScript expression inside single curly brackets ({}). In this case we are embedding the value of the name prop right after the Hello text.

+ +
<main>
+  <h1>Hello {name}!</h1>
+  <p>Visit the <a href="https://svelte.dev/tutorial">Svelte tutorial</a> to learn how to build Svelte apps.</p>
+</main>
+ +

Svelte also supports tags like {#if...}, {#each...}, and {#await...} — these examples allow you to conditionally render a portion of the markup, iterate through a list of elements, and work with async values, respectively.

+ +

The <style> section

+ +

If you have experience working with CSS, the following snippet should make sense:

+ +
<style>
+  main {
+    text-align: center;
+    padding: 1em;
+    max-width: 240px;
+    margin: 0 auto;
+  }
+
+  h1 {
+    color: #ff3e00;
+    text-transform: uppercase;
+    font-size: 4em;
+    font-weight: 100;
+  }
+
+  @media (min-width: 640px) {
+    main {
+      max-width: none;
+    }
+  }
+</style>
+ +

We are applying a style to our <h1> element. What will happen to other components with <h1> elements in them?

+ +

In Svelte, CSS inside a component's <style> block will be scoped only to that component. This works by adding a class to selected elements, which is based on a hash of the component styles.

+ +

You can see this in action by opening localhost:5000 in a new browser tab, right/Ctrl-clicking on the HELLO WORLD! label, and choosing Inspect:

+ +

Svelte starter app with devtools open, showing classes for scoped styles

+ +

When compiling the app, Svelte changes our h1 styles definition to h1.svelte-1tky8bj, and then modifies every <h1> element in our component to <h1 class="svelte-1tky8bj">, so that it picks up the styles as required.

+ +
+

Note: You can override this behavior and apply styles to a selector globally using the :global(...) modifier (see the Svelte <style> docs for more information).

+
+ +

Making a couple of changes

+ +

Now that we have a general idea of how it all fits together, we can start making a few changes.
+ At this point you can try updating your App.svelte component — for example change the <h1> element on line 6 of App.svelte so that it reads like this:

+ +
<h1>Hello {name} from MDN!</h1>
+ +

Just save your changes and the app running at localhost:5000 will be automatically updated.

+ +

A first look at Svelte reactivity

+ +

In the context of a UI framework, reactivity means that the framework can automatically update the DOM when the state of any component is changed.

+ +

In Svelte, reactivity is triggered by assigning a new value to any top level variable in a component. For example, we could include a toggleName() function in our App component, and a button to run it.

+ +

Try updating your <script> and markup sections like so:

+ +
<script>
+  export let name;
+
+  function toggleName() {
+    if (name === 'world') {
+      name = 'svelte'
+    } else {
+      name = 'world'
+    }
+  }
+</script>
+
+<main>
+  <h1>Hello {name}!</h1>
+  <button on:click={toggleName}>Toggle name</button>
+  <p>Visit the <a href="https://svelte.dev/tutorial">Svelte tutorial</a> to learn how to build Svelte apps.</p>
+</main>
+ +

Whenever the button is clicked, Svelte executes the toggleName() function, which in turn updates the value of the name variable.

+ +

As you can see, the <h1> label is automatically updated. Behind the scenes, Svelte created the JavaScript code to update the DOM whenever the value of the name variable changes, without using any virtual DOM or other complex reconciliation mechanism.

+ +

Note the use of : in on:click. That's Svelte's syntax for listening to DOM events.

+ +

Inspecting main.js: the entry point of our app

+ +

Let’s open src/main.js, which is where the App component is being imported and used. This file is the entry point for our app, and it initially looks like this:

+ +
import App from './App.svelte';
+
+const app = new App({
+  target: document.body,
+  props: {
+    name: 'world'
+  }
+});
+
+export default app;
+ +

main.js starts by importing the Svelte component that we are going to use. Then in line 3 it instantiates it, passing an option object with the following properties:

+ + + +

A look under the hood

+ +

How does Svelte manage to make all these files work together nicely?

+ +

The Svelte compiler processes the <style> section of every component and compiles them into the public/build/bundle.css file.

+ +

It also compiles the markup and <script> section of every component and stores the result in public/build/bundle.js. It also adds the code in src/main.js to reference the features of each component.

+ +

Finally the file public/index.html includes the generated bundle.css and bundle.js files:

+ +
<!DOCTYPE html>
+<html lang="en">
+<head>
+  <meta charset='utf-8'>
+  <meta name='viewport' content='width=device-width,initial-scale=1'>
+
+  <title>Svelte app</title>
+
+  <link rel='icon' type='image/png' href='/favicon.png'>
+  <link rel='stylesheet' href='/global.css'>
+  <link rel='stylesheet' href='/build/bundle.css'>
+
+  <script defer src='/build/bundle.js'></script>
+</head>
+
+<body>
+</body>
+</html>
+ +

The minified version of bundle.js weighs a little more than 3KB, which includes the "Svelte runtime" (just 300 lines of JavaScript code) and the App.svelte compiled component. As you can see, bundle.js is the only JavaScript file referenced by index.html. There are no other libraries loaded into the web page.

+ +

This is a much smaller footprint than compiled bundles from other frameworks. Take into account that, in the case of code bundles, it's not just the size of the files you have to download that matter. This is executable code that needs to be parsed, executed, and kept in memory. So this really makes a difference, especially in low-powered devices or CPU-intensive applications.

+ +

Following this tutorial

+ +

In this tutorial series you will be building a complete web application. We'll learn all the basics about Svelte and also quite a few advanced topics.

+ +

You can just read the content to get a good understanding of Svelte features, but you'll get the most out of this tutorial if you follow along coding the app with us as you go. To make it easier for you to follow each article, we provide a GitHub repository with a folder containing the source for the app as it is at the start of each tutorial.

+ +

Svelte also provides an online repl, which is a playground for live-coding Svelte apps on the web without having to install anything on your machine. We provide a repl for each article so you can start coding along right away. Let's talk a bit more about how to use these tools.

+ +

Using Git

+ +

The most popular version control system is Git, along with GitHub, a site that provides hosting for your repositories and several tools for working with them.

+ +

We'll be using GitHub so that you can easily download the source code for each article. You will also be able to get the code as it should be after completing the article, just in case you get lost.

+ +

After installing git, to clone the repository you should execute:

+ +
git clone https://github.com/opensas/mdn-svelte-tutorial.git
+ +

Then at the beginning of each article, you can just cd into the corresponding folder and start the app in dev mode to see what its current state should be, like this:

+ +
cd 02-starting-our-todo-app
+npm install
+npm run dev
+ +

If you want lo learn more about git and GitHub, we've compiled a list of links to useful guides — see Git and GitHub.

+ +
+

Note: If you just want to download the files without cloning the git repo, you can use the degit tool like this — npx degit opensas/mdn-svelte-tutorial. You can also download a specific folder with npx degit opensas/mdn-svelte-tutorial/01-getting-started. Degit won't create a local git repo, it will just download the files of the specified folder.

+
+ +

Using the Svelte REPL

+ +

A REPL (read–eval–print loop) is an interactive environment that allows you to enter commands and immediately see the results — many programming languages provide a REPL.

+ +

Svelte's REPL is much more than that. It's an online tool that allows you to create complete apps, save them online, and share with others.

+ +

It's the easiest way to start playing with Svelte from any machine, without having to install anything. It is also widely used by Svelte community. If you want to share an idea, ask for help, or report an issue, it's always extremely useful to create a REPL instance demomstrating the issue.

+ +

Let's have a quick look at the Svelte REPL and how you'd use it. It looks like so:

+ +

the svelte repl in action, showing component code on the left, and output on the right

+ +

To start a REPL, open your browser and navigate to https://svelte.dev/repl.

+ + + +

Whenever you change any file on the REPL, Svelte will recompile the app and update the Result tab. To share your app share the url. For example, here's the link for a REPL running our complete app: https://svelte.dev/repl/378dd79e0dfe4486a8f10823f3813190?version=3.23.2.

+ +
+

Note: Notice how you can specify Svelte's version in the URL. This is useful when reporting issues related to a specific version of Svelte.

+
+ +

We will provide a REPL at the beginning and end of each article so that you can start coding with us right away.

+ +
+

Note: at the moment the REPL can't handle folder names properly. If you are following the tutorial on the REPL, just create all your components inside the root folder. Then when you see a path in the code, for example import Todos from './components/Todos.svelte', just replace it with a flat URL, e.g. import Todos from './Todos.svelte'.

+
+ +

The code so far

+ +

Git

+ +

Clone the github repo (if you haven't already done it) with:

+ +
git clone https://github.com/opensas/mdn-svelte-tutorial.git
+ +

Then to get to the current app state, run

+ +
cd mdn-svelte-tutorial/01-getting-started
+ +

Or directly download the folder's content:

+ +
npx degit opensas/mdn-svelte-tutorial/01-getting-started
+ +

Remember to run npm install && npm run dev to start your app in development mode.

+ +

REPL

+ +

To code along with us using the REPL, start at

+ +

https://svelte.dev/repl/fc68b4f059d34b9c84fa042d1cce586c?version=3.23.2

+ +

Summary

+ +

This brings us to the end of our initial look at Svelte, including how to install it locally, create a starter app, and how the basics work. In the next article we'll start building our first proper application — a todo list. Before we do that, however, let's recap some of the things we’ve learned.

+ +

In Svelte:

+ + + +

{{PreviousMenuNext("Learn/Tools_and_testing/Client-side_JavaScript_frameworks/Vue_resources","Learn/Tools_and_testing/Client-side_JavaScript_frameworks/Svelte_todo_list_beginning", "Learn/Tools_and_testing/Client-side_JavaScript_frameworks")}}

+ +

於此模組中

+ + -- cgit v1.2.3-54-g00ecf