├── chapter2
├── Actions.md
├── Element-directives
│ ├── README.md
│ ├── class_name.md
│ ├── in_out.md
│ ├── use_action.md
│ ├── animate.md
│ └── transition_fn.md
├── Text expressions.md
├── svelte-body.md
├── Tags.md
├── svelte-head.md
├── HTML expressions.md
├── svelte-component.md
├── svelte-self.md
├── Component event.md
├── Component Bindings.md
├── svelte-options.md
├── If blocks.md
├── svelte-window.md
├── debug.md
├── Await block.md
├── Attributes.md
├── Each blocks.md
├── Dom event.md
├── Slots.md
└── Element bindings.md
├── runtime
├── README.md
├── Readme.md
├── svelte.easing.md
├── svelte.register.md
├── svelte.animate.md
├── svelte.motion.md
├── svelte.store.md
└── transition.md
├── .gitignore
├── .bookignore
├── chapter1
├── README.md
├── style.md
├── module.md
├── script.md
└── component-format.md
├── api-compile-time
├── svelte.VERSION.md
├── svelte.parse.md
├── README.md
├── svelte.walk.md
├── svelte.preprocess.md
└── svelte.compile.md
├── deploy.sh
├── README.md
└── SUMMARY.md
/chapter2/Actions.md:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/runtime/README.md:
--------------------------------------------------------------------------------
1 | ## RUN TIME
--------------------------------------------------------------------------------
/runtime/Readme.md:
--------------------------------------------------------------------------------
1 | ## RUN TIME
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .history
2 | .idea
3 | .DS_Store
4 | _book
--------------------------------------------------------------------------------
/runtime/svelte.easing.md:
--------------------------------------------------------------------------------
1 | ### svelte/animate
2 |
3 | - `TODO` 将会有很好用的小交互式的组件,也可能会显示其他的功能
--------------------------------------------------------------------------------
/.bookignore:
--------------------------------------------------------------------------------
1 | # see https://gitbookio.gitbooks.io/docs-toolchain/structure.html
2 | deploy.sh
--------------------------------------------------------------------------------
/chapter2/Element-directives/README.md:
--------------------------------------------------------------------------------
1 | ### Element directives
2 |
3 | 除了属性之外,元素还可以有指令,这些指令以某种方式控制元素的行为。
--------------------------------------------------------------------------------
/chapter1/README.md:
--------------------------------------------------------------------------------
1 | ### 组件格式
2 |
3 | 组件是Svelte应用的内置区块,是使用HTML的扩展格式的语言写成.svelte文件
4 |
5 | 一共分为三个区域、script、style和标签(选填)
--------------------------------------------------------------------------------
/api-compile-time/svelte.VERSION.md:
--------------------------------------------------------------------------------
1 | ###svelte.VERSION
2 |
3 | 当前的版本,跟 package.json 中设置的一样。
4 |
5 | ```js
6 | const svelte = require('svelte/compiler');
7 | console.log(`running svelte version ${svelte.VERSION}`);
8 | ```
--------------------------------------------------------------------------------
/chapter2/Text expressions.md:
--------------------------------------------------------------------------------
1 | ### Text expressions
2 |
3 |
4 | ```html
5 | {expression}
6 | ```
7 |
8 | 文本也可以包含 javascript 表达式:
9 |
10 | ```html
11 |
Hello {name}!
12 | {a} + {b} = {a + b}.
13 | ```
--------------------------------------------------------------------------------
/chapter2/svelte-body.md:
--------------------------------------------------------------------------------
1 | ### ``
2 |
3 | ```html
4 |
5 | ```
6 |
7 | 与 `` 一样,此元素允许你在 `document.body` 上添加事件监听,用来监听`mouseenter` 和 `mouseleave` 等等不会在 window 上触发的事件 。
8 |
--------------------------------------------------------------------------------
/chapter2/Tags.md:
--------------------------------------------------------------------------------
1 | ### Tags
2 |
3 | 小写标签(如``)表示常规的 HTML 元素。而大写的标签(如`
`或``)则表示一个组件。
4 |
5 | ```html
6 |
9 |
10 |
11 |
12 |
13 | ```
--------------------------------------------------------------------------------
/chapter2/svelte-head.md:
--------------------------------------------------------------------------------
1 | ### ``
2 |
3 | ```html
4 |
5 | ```
6 |
7 | `` 可以让你在 `document.head` 中插入元素。在服务端渲染时,`head` 部分内容单独地暴露给主 `html`。
8 |
9 | ```html
10 |
11 |
12 |
13 | ```
--------------------------------------------------------------------------------
/deploy.sh:
--------------------------------------------------------------------------------
1 | # !/bin/bash
2 |
3 | git push origin :gh-pages
4 | rm -rf ./_book
5 | gitbook build
6 | cd _book
7 | git init
8 | git checkout -b gh-pages
9 | git remote add origin https://github.com/sveltejs-cn/test.git
10 | git add --all
11 | git commit -m 'publish'
12 | git push origin gh-pages
13 |
--------------------------------------------------------------------------------
/chapter2/HTML expressions.md:
--------------------------------------------------------------------------------
1 | ### HTML expressions
2 |
3 | ```html
4 | {@html expression}
5 | ```
6 |
7 | 在文本表达式中,像`<`和`>`这样的字符被转义。而在 HTML 表达式中则不会被转义。
8 |
9 | > Svelte 不会在注入 HTML 之前过滤表达式。所以,如果是不受信任源的数据,你必须先过滤它。否则,你的用户将会面临 XSS 攻击。
10 |
11 | ```html
12 |
13 |
{post.title}
14 | {@html post.content}
15 |
16 | ```
17 |
18 |
--------------------------------------------------------------------------------
/chapter2/svelte-component.md:
--------------------------------------------------------------------------------
1 | ### ``
2 |
3 | ```html
4 |
5 | ```
6 |
7 | `` 会使用指定了 `this` 属性的组件构造函数来动态的渲染组件,当属性发生改变的时候,该组件将被销毁并重新创建。
8 |
9 | ```html
10 |
11 | ```
12 |
13 | 如果 `this` 的值能够被转换成 `false`(即 `Boolean(this) 为 false`) ,则不会渲染组件。
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ### 前言
2 |
3 | > 临时说明:这是一个正在完善中的文档。请原谅这其中有一些错误或问题,也请不要怯于在[Discord Chatroom](https://svelte.dev/chat)寻求帮助。相关[Tutorial](https://svelte.dev/tutorial/basics)已经完备,从这里开始吧。
4 |
5 | 这是一个包含相关详细API的文档。它旨在成为已经熟悉Svelte的人的资源。
6 | 如果那不是您想要的,在查阅此文档之前您可能更愿意访问[Tutorial](https://svelte.dev/tutorial/basics)或[Example](https://svelte.dev/examples#hello-world)。
7 |
8 |
9 |
10 |
11 | ### 感谢
--------------------------------------------------------------------------------
/chapter2/svelte-self.md:
--------------------------------------------------------------------------------
1 | ### ``
2 |
3 | `` 元素允许递归的方式包含自身。
4 |
5 | `` 不能出现在标签的最外层,它必须是在 `if` 或 `each` 代码块中,防止陷入无限循环。
6 |
7 |
8 | ```html
9 |
12 |
13 | {#if count > 0}
14 | counting down... {count}
15 |
16 | {:else}
17 | lift-off!
18 | {/if}
19 | ```
--------------------------------------------------------------------------------
/chapter2/Component event.md:
--------------------------------------------------------------------------------
1 | ## Component events
2 |
3 | ```html
4 | on:eventname={handler}
5 | ```
6 |
7 | 组件可以使用`createEventDispatcher`或转发 DOM 事件来 emit 事件。
8 |
9 | 监听组件的事件,和监听 html 事件一模一样。
10 |
11 | ```html
12 |
13 | ```
14 |
15 | 和 DOM 事件一样,如果使用`on:`指令而没有值,则组件将转发事件,这意味着组件的使用者可以监听这个事件。
16 |
17 | ```html
18 |
19 | ```
20 |
21 |
--------------------------------------------------------------------------------
/api-compile-time/svelte.parse.md:
--------------------------------------------------------------------------------
1 | ### svelte.parse
2 |
3 | ```js
4 | ast: object = svelte.parse(
5 | source: string,
6 | options?: {
7 | filename?: string,
8 | customElement?: boolean
9 | }
10 | )
11 | ```
12 |
13 | `parse` 函数用来解析一个组件并返回它的抽象语法树。
14 |
15 | ```js
16 | const svelte = require('svelte/compiler');
17 |
18 | const ast = svelte.parse(source, { filename: 'App.svelte' });
19 | ```
20 |
21 | 与编译用的 `generate: false` 选项不同的是,这个只会进行解析,不会做对这个组件做更多的验证或分析。
--------------------------------------------------------------------------------
/api-compile-time/README.md:
--------------------------------------------------------------------------------
1 | 通常,你不会直接和 Svelte 编译器打交道,但是你可以在构建系统中通过打包插件(bundler plugins)的形式把它整合进去:
2 |
3 | - 对于 [Rollup](https://rollupjs.org/) 用户可以使用 [rollup-plugin-svelte](https://github.com/rollup/rollup-plugin-svelte)
4 | - 对于 [webpack](https://webpack.js.org/) 用户可以使用 [svelte-loader](https://github.com/rollup/rollup-plugin-svelte)
5 | - 对于 [Parcel](https://parceljs.org/) 用户可以使用 [parcel-plugin-svelte](https://github.com/DeMoorJasper/parcel-plugin-svelte)
6 |
7 | 尽管如此,鉴于打包插件通常会暴露编译器选项(compiler option)给你,知道如何使用编译器会非常有帮助。
8 |
--------------------------------------------------------------------------------
/chapter2/Component Bindings.md:
--------------------------------------------------------------------------------
1 | ### Component bindings
2 |
3 | ```html
4 | bind:property={variable}
5 | ```
6 |
7 | ```html
8 | bind:this={component_instance}
9 | ```
10 |
11 | 你可以使用相同的机制给组件绑定属性。
12 |
13 | ```html
14 |
15 | ```
16 |
17 | 组件还支持`bind:this`,允许你以动态数据的方式与组件实例进行交互。
18 |
19 | > 注意,当按钮首次渲染的时候,我们不能使用`{cart.empty}`,因为`cart`还是`undefined`,这样会抛出异常。
20 |
21 | ```html
22 |
23 |
24 |
27 | ```
28 |
29 |
--------------------------------------------------------------------------------
/chapter2/Element-directives/class_name.md:
--------------------------------------------------------------------------------
1 | ### class:*name*
2 | ```
3 | class:name={value}
4 | ```
5 | ```
6 | class:name
7 | ```
8 | 这种 `class:` 指令提供了一个更便捷的方式来切换元素上的 `class`。
9 | ```html
10 |
11 | ...
12 | ...
13 |
14 |
15 | ...
16 |
17 |
18 | ...
19 | ```
--------------------------------------------------------------------------------
/chapter2/svelte-options.md:
--------------------------------------------------------------------------------
1 | ### `svelte-options`
2 |
3 | ```html
4 |
5 | ```
6 |
7 | ```html
8 |
9 | ```
10 |
11 | `` 提供了一个地方去指定每一个组件的编译的选项,相关内容在编译器部分有详细介绍。支持的选项有:
12 |
13 | - `immutable={true}` - 表示你将不会使用可变的数据,如果值发生改变的时候,编译器会依据指向是否相等的简单检查
14 | - `immutable={false}` - 默认值。对于可变对象是否发生了变化,`Svelte` 会更加保守
15 | - `accessors={true}` - 为组件的 `props` 添加 `getters` 和 `setters`
16 | - `accessors={false}` — 默认值
17 | - `namespace="..."` - 指定命名空间,表明这个组件将会在哪里被使用,最常见的是`svg`
18 | - `tag="..."` - 将此组件编译为自定义元素时使用的名称
--------------------------------------------------------------------------------
/chapter2/If blocks.md:
--------------------------------------------------------------------------------
1 | ### If blocks
2 |
3 | ```html
4 | {#if expression}...{/if}
5 |
6 | {#if expression}...{:else if expression}...{/if}
7 |
8 | {#if expression}...{:else}...{/if}
9 | ```
10 |
11 | 条件渲染的内容,可以被包裹在一个 `if` 块中。
12 |
13 | ```html
14 | {#if answer === 42}
15 | what was the question?
16 | {/if}
17 | ```
18 |
19 | 可以用`{:else if expression}`添加额外的条件,也可以选择是否以`{:else}`结尾。
20 |
21 | ```html
22 | {#if porridge.temperature > 100}
23 | too hot!
24 | {:else if 80 > porridge.temperature}
25 | too cold!
26 | {:else}
27 | just right!
28 | {/if}
29 | ```
30 |
31 |
--------------------------------------------------------------------------------
/chapter1/style.md:
--------------------------------------------------------------------------------
1 | ### style
2 |
3 | `
12 | ```
13 | 可以通过使用`:global(...)`修饰符将样式作用域全局。
14 | ```html
15 |
28 | ```
--------------------------------------------------------------------------------
/chapter1/module.md:
--------------------------------------------------------------------------------
1 | ### <script content="module">
2 |
3 | 具有`context =“module”`属性的`
16 |
17 |
21 | ```
--------------------------------------------------------------------------------
/chapter2/Element-directives/in_out.md:
--------------------------------------------------------------------------------
1 | ### in:*fn*/out:*fn*
2 | ```
3 | in:fn
4 | ```
5 | ```
6 | in:fn={params}
7 | ```
8 | ```
9 | in:fn|local
10 | ```
11 | ```
12 | in:fn|local={params}
13 | ```
14 | ```
15 | out:fn
16 | ```
17 | ```
18 | out:fn={params}
19 | ```
20 | ```
21 | out:fn|local
22 | ```
23 | ```
24 | out:fn|local={params}
25 | ```
26 | 和`transition`类似,但是只适用于元素和进入(`in:`)和元素的离开(`out:`)。
27 |
28 | ```js
29 | {#if visible}
30 |
31 | flies in, fades out
32 |
33 | {/if}
34 | ```
35 |
36 | 和`transition`不一样的是,transitions 带有`in: ` 或 `out:` 指令就不是双向的。进入的过渡执行的过程中,切换至离开动画,进入动画会继续执行,且与离开动画并存。如果之前离开的动画还未执行完成,会重新开启离开的动画。
--------------------------------------------------------------------------------
/runtime/svelte.register.md:
--------------------------------------------------------------------------------
1 | ### svelte/register
2 |
3 | 在 Node.js 中渲染 `Svelte` 组件无需绑定值,使用 `require('svelte/register')`即可。之后,你可以使用 `require` 来引入任何 `.svelte` 文件。
4 |
5 | ```js
6 | require('svelte/register');
7 |
8 | const App = require('./App.svelte').default;
9 |
10 | ...
11 |
12 | const { html, css, head } = App.render({ answer: 42 });
13 | ```
14 |
15 | > `.default` 是必要的,因为我们是从原生 JavaScript 模块转换为 Node 识别的 CommonJS 模块。请注意,如果你的组件导入 JavaScript 模块,它们将无法在 Node 中加载,你得需要使用打包插件。
16 |
17 |
18 | 如果要设置编译选项或者使用自定义文件扩展名,可函数式调用 `register` 钩子函数来设置。
19 |
20 | ```js
21 | require('svelte/register')({
22 | extensions: ['.customextension'], // defaults to ['.html', '.svelte']
23 | preserveComments: true
24 | });
25 | ```
--------------------------------------------------------------------------------
/chapter2/svelte-window.md:
--------------------------------------------------------------------------------
1 | ### ``
2 |
3 | ```html
4 |
5 | ```
6 | ```html
7 |
8 | ```
9 |
10 | `` 可以给 `window` 对象添加事件监听,通过这种方式组件被移除时你不需要再手动移除监听器,服务端渲染时你也不需要手动检测 `window` 对象。
11 |
12 | ```html
13 |
18 |
19 |
20 | ```
21 | 你也可以绑定以下这些属性:
22 |
23 | - `innerWidth`
24 | - `innerHeight`
25 | - `outerWidth`
26 | - `outerHeight`
27 | - `scrollX`
28 | - `scrollY`
29 | - `online` - `window.navigator.onLine`的别名
30 |
31 | ```html
32 |
33 | ```
34 |
35 | 除 `scrollX` 和 `scrollY` 之外其余都是只读的。
--------------------------------------------------------------------------------
/chapter2/debug.md:
--------------------------------------------------------------------------------
1 | ### {@debug}
2 |
3 | ```html
4 | {@debug}
5 | ```
6 |
7 | ```html
8 | {@debug var1, var2, ..., varN}
9 | ```
10 |
11 | `{@debug ...}` 可以替代 `console.log(...)` 。每当特定值在修改的时候,它会记录下来,而且如果你的 devtools 打开的状态下,会阻断代码的执行。
12 |
13 | ```html
14 |
20 |
21 | {@debug user}
22 |
23 | Hello {user.firstname}!
24 | ```
25 |
26 | 它接受以逗号分隔的变量名称列表(不是任意表达式)。
27 |
28 | ```html
29 |
30 | {@debug user}
31 | {@debug user1, user2, user3}
32 |
33 |
34 | {@debug user.firstname}
35 | {@debug myArray[0]}
36 | {@debug !isReady}
37 | {@debug typeof user === 'object'}
38 | ```
39 |
40 | 不带任何参数的 `{@debug}` 将插入一个 `debugger` ,当任何状态发生改变的时候都会触发,而不是特定的值。
--------------------------------------------------------------------------------
/api-compile-time/svelte.walk.md:
--------------------------------------------------------------------------------
1 | ### svelte.walk
2 |
3 | ```
4 | walk(ast: Node, {
5 | enter(node: Node, parent: Node, prop: string, index: number)?: void,
6 | leave(node: Node, parent: Node, prop: string, index: number)?: void
7 | })
8 | ```
9 |
10 | `walk` 函数提供了一种方法,可以通过编译器自己内置的 [estree-walker](https://github.com/Rich-Harris/estree-walker) 实例来遍历解析器生成的抽象语法树的方法。
11 |
12 | ```
13 | const svelte = require('svelte/compiler');
14 | svelte.walk(ast, {
15 | enter(node, parent, prop, index) {
16 | do_something(node);
17 | if (should_skip_children(node)) {
18 | this.skip();
19 | }
20 | },
21 | leave(node, parent, prop, index) {
22 | do_something_else(node);
23 | }
24 | });
25 | ```
26 |
27 | 遍历器需要传入一个抽象语法树以及一个对象(包含可选的两个方法:`enter` 和 `leave`)。对于每个节点,`enter` 会被调用(如果传入的对象中存在这个方法的话)。
28 | 接下来,该节点的每个子节点都会被遍历(除非在 `enter` 方法中调用 `this.skip()` 方法)。
29 | 遍历完成后,`leave` 方法会在这个节点上被调用。
--------------------------------------------------------------------------------
/runtime/svelte.animate.md:
--------------------------------------------------------------------------------
1 | ### svelte/animate
2 |
3 | `svelte/animate` 模块导出一个用于 svelte 动画的函数。
4 |
5 | #### filp
6 |
7 | ```
8 | animate:flip={params}
9 | ```
10 | `flip` 函数计算元素的开始和结束位置,并通过转换`x`、`y`坐标对他们设置动画。`flip` 主张的是[First,Last,Invert,Play](https://aerotwist.com/blog/flip-your-animations/)。
11 |
12 | `flip` 接受下面这些参数:
13 | - `delay` (`number`,默认 0) - 开始前的毫秒数
14 | - `duration` (`number`|`function`, 默认 `d => Math.sqrt(d) * 120`) - 参见下文
15 | - `easing` (`function`, `cubicOut`) - 缓动函数
16 | `duration`可以通过下面任一形式定义:
17 | - 一个`number`,以毫秒为单位
18 | - 一个函数,`distance: number => duration: number`,接受`distance`的元素将以像素为单位进行移动,并以毫秒为单位返回持续的时间。支持指定相对于每个元素移动的持续时间。
19 |
20 | ```html
21 |
27 |
28 | {#each list as n (n)}
29 |
30 | {n}
31 |
32 | {/each}
33 | ```
34 |
35 | 你可以在 [animate tutorial](https://svelte.dev/tutorial/animate) 看到完整的示例。
--------------------------------------------------------------------------------
/chapter2/Await block.md:
--------------------------------------------------------------------------------
1 | ### Await
2 |
3 | ```html
4 | {#await expression}...{:then name}...{:catch name}...{/await}
5 | ```
6 | ```html
7 | {#await expression}...{:then name}...{/await}
8 | ```
9 | ```html
10 | {#await expression then name}...{/await}
11 | ```
12 |
13 | `await` 可以让你处理 Promise 的三种状态 `pending`, `fulfilled`, `rejected`
14 |
15 | ```html
16 | {#await promise}
17 |
18 | waiting for the promise to resolve...
19 | {:then value}
20 |
21 | The value is {value}
22 | {:catch error}
23 |
24 | Something went wrong: {error.message}
25 | {/await}
26 | ```
27 |
28 | 如果`Promise`状态变为`reject`,或者发生错误时,你不需要渲染任何东西,则`catch` 块可以被忽略。
29 |
30 | ```html
31 | {#await promise}
32 |
33 | waiting for the promise to resolve...
34 | {:then value}
35 |
36 | The value is {value}
37 | {/await}
38 | ```
39 | 如果你不关心`pending`状态,也可以省略初始化的状态。
40 |
41 | ```html
42 | {#await promise then value}
43 | The value is {value}
44 | {/await}
45 | ```
46 |
47 |
--------------------------------------------------------------------------------
/chapter2/Attributes.md:
--------------------------------------------------------------------------------
1 | ### Attributes
2 |
3 | 默认情况下,模板的属性,与其对应的 HTML 属性完全相同。
4 |
5 | ```html
6 |
7 |
8 |
9 | ```
10 |
11 | 与 HTML 一样,属性值可以不加引号。
12 |
13 | ```html
14 |
15 | ```
16 |
17 | 属性值可以包含`javascript`的表达式:
18 |
19 | ```html
20 | page {p}
21 | ```
22 |
23 | 或者,属性值本身就是`javascript`的表达式:
24 |
25 | ```html
26 |
27 | ```
28 |
29 |
32 |
33 | 某些`javascript`表达式可能会引起常规的 HTML 语法高亮失效,所以可以使用**引号**(来解决该问题),使用**引号**不会影响表达式的正常解析
34 |
35 | ```html
36 |
37 | ```
38 | 当属性名和表达式相同(`name={name}`)时,可以用`{name}`来代替。
39 |
40 | ```html
41 |
42 |
43 |
44 | ```
45 |
46 |
47 |
48 | 属性展开特性(*Spread attributes*)可以将多个属性和值一次传递给元素或者组件(类似JSX)
49 |
50 | 元素或组件可以具有多个展开的属性,其中可以包含常规的属性。
51 |
52 | ```html
53 |
54 | ```
55 |
56 |
--------------------------------------------------------------------------------
/chapter2/Element-directives/use_action.md:
--------------------------------------------------------------------------------
1 | ### use:*action*
2 | ```js
3 | use:action
4 | ```
5 | ```js
6 | use:action={parameters}
7 | ```
8 | ```js
9 | action = (node: HTMLElement, parameters: any) => {
10 | update?: (parameters: any) => void,
11 | destroy?: () => void
12 | }
13 | ```
14 | `Actions` 是一个元素被创建时会调用的函数集。函数集中的函数会返回一个包含 `destroy` 的钩子函数的对象,`destroy` 会在元素被卸载的时候被调用。
15 |
16 | ```html
17 |
28 |
29 |
30 | ```
31 |
32 | `Action` 可以有多个参数,如果返回对象有 `update` 方法,只要这些参数发生改变,这个方法会在 Svelte 更新标签之后被立即调用。
33 | ```html
34 |
51 |
52 |
53 | ```
54 | > 不用担心如果我们为每个组件实例都重新声明了 `foo` 会有什么影响—— Svelte 会从组件的定义中移除不依赖于本地状态的函数。
--------------------------------------------------------------------------------
/chapter2/Each blocks.md:
--------------------------------------------------------------------------------
1 | ## Each blocks
2 |
3 | ```html
4 | {#each expression as name}...{/each}
5 | ```
6 | ```html
7 | {#each expression as name, index}...{/each}
8 | ```
9 | ```html
10 | {#each expression as name, index (key)}...{/each}
11 | ```
12 | ```html
13 | {#each expression as name}...{:else}...{/each}
14 | ```
15 |
16 | 可以使用`each`来遍历一个列表数据。
17 |
18 | ```html
19 | Shopping list
20 |
21 | {#each items as item}
22 | - {item.name} x {item.qty}
23 | {/each}
24 |
25 | ```
26 |
27 | 每一个`each`块,都可指定`index` 。 `index`的值和`array.map(...)`的回调方法的第二个参数相同。
28 |
29 | ```html
30 | {#each items as item, i}
31 | {i + 1}: {item.name} x {item.qty}
32 | {/each}
33 | ```
34 | 如果你指定了`key`(必须是该列表中每一项的唯一标示),Svelte 会在数据发生变更的时候,对列表进行 diff(而不只是在结尾添加或删除项)。
35 | `key`可以是任何对象,但是推荐使用字符或数字,因为它们在对象发生变化的时候仍保持不变。
36 |
37 | ```html
38 | {#each items as item, i (item.id)}
39 | {i + 1}: {item.name} x {item.qty}
40 | {/each}
41 | ```
42 |
43 | 你可以在`each`中使用解构的语法。
44 | ```html
45 | {#each items as { id, name, qty }, i (id)}
46 | {i + 1}: {name} x {qty}
47 | {/each}
48 | ```
49 |
50 | 每个`each`也可以有一个`{:else}`部分,如果列表为空,则会渲染该部分的内容。
51 |
52 | ```html
53 | {#each todos as todo}
54 | {todo.text}
55 | {:else}
56 | No tasks today!
57 | {/each}
58 | ```
59 |
60 |
--------------------------------------------------------------------------------
/chapter2/Dom event.md:
--------------------------------------------------------------------------------
1 | ### DOM events
2 |
3 | ```html
4 | on:eventname={handler}
5 | ```
6 |
7 | ```html
8 | on:eventname|modifiers={handler}
9 | ```
10 |
11 | 你可以使用`on:`的指令来监听 DOM 事件:
12 |
13 | ```html
14 |
21 |
22 |
25 | ```
26 | 内联方式声明的处理函数,并不会影响性能。与其他属性一样,为了可以语法高亮显示,指令的值可以用引号包裹。
27 | ```html
28 |
31 | ```
32 |
33 | 使用 | 字符来向DOM事件添加修饰符。
34 |
35 | 可以使用以下修饰符:
36 |
37 | - `preventDefault`: 在 handeler 执行前,调用 `event.preventDefault()`
38 | - `stopPropagation`: 调用`event.stopPropagation()`,阻止事件冒泡
39 | - `passive`: 提高触摸/滚动事件的滚动性能( Svelte 会在支持地方默认使用它)
40 | - `capture`:在捕获阶段而不是冒泡阶段,触发事件处理函数
41 | - `once`: 事件处理函数被执一次后会被移除
42 |
43 | 修饰符可以使用 `|` 链接起来:`on:click|once|capture={...}`
44 |
45 | ```html
46 |
50 | ```
51 |
52 | 如果 `on:` 指令没有值,则组件会转发这个事件,意味着组件的使用者,可以监听这个事件。
53 |
54 | ```html
55 |
58 | ```
59 |
60 | 对于同样的事件,可以有多个事件监听者。
61 |
62 | ```html
63 |
73 |
74 |
75 | ```
--------------------------------------------------------------------------------
/chapter2/Slots.md:
--------------------------------------------------------------------------------
1 | ### Slots
2 | ```html
3 |
4 | ```
5 | ```html
6 |
7 | ```
8 | ```html
9 |
10 | ```
11 | 和原生的 DOM 元素一样,组件在使用的时候也可以有子内容。
12 |
13 | 子内容会替代在组件中使用 `` 包裹的内容,如果没有向插槽提供内容的时候,`` 包裹的内容会作为默认内容返回。
14 |
15 | ```html
16 |
17 |
18 | this is some child content
19 |
20 |
21 |
22 |
23 |
24 | this will be rendered if someone does
25 |
26 |
27 | ```
28 |
29 | 具名插槽允许开发者指定特定插槽的内容,当然他们也可以有默认内容。
30 |
31 | ```html
32 |
33 |
34 | Hello
35 | Copyright (c) 2019 Svelte Industries
36 |
37 |
38 |
39 |
40 |
No header was provided
41 |
Some content between header and footer
42 |
43 |
44 | ```
45 |
46 | 插槽可渲染零次或多次,可通过 `props` 把值回传给父级。父级可以可以通过 `let:` 指令把值传给插槽模版。
47 |
48 | 常用简洁的方式来写- `let:item` 表示 `let:item={item}` ,而且 `` 等同于 ``。
49 |
50 | ```html
51 |
52 |
53 | {item.text}
54 |
55 |
56 |
57 |
58 | {#each items as item}
59 | -
60 |
61 |
62 | {/each}
63 |
64 | ```
65 |
66 | 具名插槽也可以暴露一个值给父级,通过在元素上使用对应的 `let:` 指令获取暴露的属性。
67 |
68 | ```html
69 |
70 |
71 | {item.text}
72 | Copyright (c) 2019 Svelte Industries
73 |
74 |
75 |
76 |
77 | {#each items as item}
78 | -
79 |
80 |
81 | {/each}
82 |
83 |
84 |
85 | ```
--------------------------------------------------------------------------------
/chapter1/script.md:
--------------------------------------------------------------------------------
1 | ### script
2 | `
43 | ```
44 |
45 | 2. 赋值是`响应式`的
46 |
47 | 要改变组件的状态`state`并触发组件的重新渲染,只需要给已申明变量重新赋值
48 |
49 | 使用表达式(count += 1)更新count的值和属性赋值(obj.x = y)是等效的。
50 |
51 | 因为`Svelte`的`响应式特性`是基于赋值的,因此使用`.push`和`.splice`将不会自动触发更新。有关解决此问题的选项,可以参考此[教程](https://svelte.dev/tutorial/updating-arrays-and-objects)
52 |
53 | ```html
54 |
63 | ```
64 |
65 | 3 使用`$:`将表达式设为响应式的
66 |
67 | 任何顶层的表达式(不包含块级作用域里面和函数申明里面)都可以通过在前面添加`$:`前缀来使其成为响应式的。这是js的标签语法。只要它们所依赖的值发生更改,响应式语句就会在组件更新之前立即运行。
68 |
69 | 如果一个表达式是一条由完全未申明的变量组成的赋值语句,Svelte会替你插入一个let申明。
70 |
71 | ```html
72 |
84 | ```
85 |
86 | 4 为Store添加$前缀来访问它们的值
87 |
88 | 任何时候,对于任意一个Store,你都可以在组件内部为该Store的引用添加$字符前缀来访问它的值。这会导致Svelte申明有前缀的变量,并且会设置一个Store订阅然后再适当的时候会取消这个订阅。
89 |
90 | 要注意,必须在组件的顶层申明Store,并且不可以包含在if代码块中
91 |
92 | 本地申明的变量(不代指store的值)不允许还有$前缀
--------------------------------------------------------------------------------
/chapter2/Element-directives/animate.md:
--------------------------------------------------------------------------------
1 | ### animate:
2 |
3 | ```
4 | animate:name
5 | ```
6 | ```
7 | animate:name={params}
8 | ```
9 | ```html
10 | animation = (node: HTMLElement, { from: DOMRect, to: DOMRect } , params: any) => {
11 | delay?: number,
12 | duration?: number,
13 | easing?: (t: number) => number,
14 | css?: (t: number, u: number) => string,
15 | tick?: (t: number, u: number) => void
16 | }
17 | ```
18 | ```html
19 | DOMRect {
20 | bottom: number,
21 | height: number,
22 | left: number,
23 | right: number,
24 | top: number,
25 | width: number,
26 | x: number,
27 | y:number
28 | }
29 | ```
30 | 当一个通过 `each` 生成的块发生重排的时候,`Animation` 才会执行。当元素被删除的时候动画不会执行,只有 `each` 的列表数据发生重排的时候才会执行 `Animation` 。且`animate:` 指令只能在each块的直接子元素上。
31 |
32 | ```html
33 |
34 | {#each list as item, index (item)}
35 | {item}
36 | {/each}
37 | ```
38 | `animate` 可以是内置的 `animation` 方法或自定义的 `animation` 方法。
39 |
40 | #### Animation参数
41 | 和 `Actions` 还有 `transitions` 一样,`Animation` 也有参数。
42 |
43 | ```html
44 | {#each list as item, index (item)}
45 | {item}
46 | {/each}
47 | ```
48 | 双大括号 `{{curlies}}` 不是特殊的语法,只是一个在表达式标签中包含一个对象字面量
49 |
50 | #### 自定义Animation函数
51 |
52 |
53 | ```html
54 |
73 |
74 | {#each list as item, index (item)}
75 | {item}
76 | {/each}
77 | ```
78 |
79 | `Animations` 可以是自定义的函数,函数的参数分别是 `node` 、`animation` 对象、及一个任何格式的参数。`animation` 对象参数包含 `from` 和 `to` 属性,且都包含了能描述元素的物理几何位置 `start` 和 `end` 的 DOMRect 属性。当列表数据发生重排,且 DOM 已经完成更新,`from` 属性表示的是元素在起始位置的 DOMRect,`to` 属性表示的是元素结束位置的 DOMRect。
80 |
81 | 如果返回的对象有 css 的方法,`Svelte` 会为这个元素创建并执行一个 css 的动画。
82 |
83 | 当缓动动画执行的时候,`t` 从 `0` 变化到 `1`,且会被作为参数传入到css方法中,参数 `u` 等同于 `1-t`。
84 |
85 | 在动画开始之前,`animate` 会被通过不同的参数 `t` 和 `u` 反复的调用。
86 |
87 | 自定义的 `animation` 函数同样可以返回 `tick` 函数,在动画的执行过程中被调用,同样的也是以参数 `t` 和 `u`。
88 |
89 | ```html
90 |
111 |
112 | {#each list as item, index (item)}
113 | {item}
114 | {/each}
115 | ```
116 | > 尽可能的使用 css 而不是 `tick` 函数,之所以这样做-是因为 css 动画可以在主线程上进行,防止在低端设备出现卡顿。
117 |
--------------------------------------------------------------------------------
/SUMMARY.md:
--------------------------------------------------------------------------------
1 | # Summary
2 |
3 | * [开始之前](README.md)
4 | * [组件格式](chapter1/README.md)
5 | * [<script>](chapter1/script.md)
6 | * [<script content="module">](chapter1/module.md)
7 | * [<style>](chapter1/style.md)
8 | * [模版语法](chapter2/README.md)
9 | * [Tags](chapter2/Tags.md)
10 | * [Attributes](chapter2/Attributes.md)
11 | * [Text expression](chapter2/Text expressions.md)
12 | * [HTML expression](chapter2/HTML expressions.md)
13 | * [If blocks](chapter2/If blocks.md)
14 | * [Each blocks](chapter2/Each blocks.md)
15 | * [Await blocks](chapter2/Await block.md)
16 | * [DOM events](chapter2/Dom event.md)
17 | * [Component events](chapter2/Component event.md)
18 | * [Element bindings](chapter2/Element bindings.md)
19 | * [Binding related elements](chapter2/Element bindings.md#绑定相关元素)
20 | * [Binding