赞
踩
构建 web 应用程序的工具
。构建/编译阶段
将你的应用程序转换为理想的 JavaScript 应用,而不是在 运行阶段 解释应用程序的代码。这意味着你不需要为框架所消耗的性能付出成本,并且在应用程序首次加载时没有额外损失。{}
<script>
let name = 'world';
</script>
<h1>Hello {name}!</h1>
对比Vue:{{}}
=> {}
{}
<script>
let src = 'tutorial/image.gif';
let name = 'Rick Astley';
</script>
<img {src} alt="{name} dances.">
对比Vue::src = ""
=> src={}
注意:组件名称 Nested 的首字母大写。采用此约定是为了使我们能够区分用户自定义的组件和一般的 HTML 标签。
<script>
import Nested from './Nested.svelte';
</script>
<style>
p {
color: purple;
font-family: 'Comic Sans MS', cursive;
font-size: 2em;
}
</style>
<p>This is a paragraph.</p>
<Nested/>
@html
<script>
let string = `this string contains some <strong>HTML!!!</strong>`;
</script>
<p>{@html string}</p>
对比Vue:v-html
=> @html
on:click={}
<script>
let count = 0;
function handleClick() {
count++;
// event handler code goes here
}
</script>
<button on:click={handleClick}>
Clicked {count} {count === 1 ? 'time' : 'times'}
</button>
对比Vue:@click=""
=> on:click={}
$:
<script>
let count = 0;
$: doubled = count * 2
function handleClick() {
count += 1;
}
</script>
<button on:click={handleClick}>
Clicked {count} {count === 1 ? 'time' : 'times'}
</button>
<p>
{count} doubled is {doubled}
</p>
对比Vue:computed
=> $:
$:{}
<script> let count = 0; $: console.log(`the count is ${count}`) $: { console.log(`the count is ${count}`) alert(`the count is ${count}`) } $: if (count >= 2){ console.log(`the count is ${count}`) alert(`count is dangerously high ${count}`) } function handleClick() { count += 1; } </script> <button on:click={handleClick}> Clicked {count} {count === 1 ? 'time' : 'times'} </button>
被更新的变量的名称必须出现在赋值语句的左侧
。<script> let numbers = [1, 2, 3, 4]; function addNumber() { numbers = [...numbers, numbers.length + 1] numbers[numbers.length] = numbers.length + 1; } const obj = {foo: {bar: 1}} const foo = obj.foo; foo.bar = 'baz'; $: sum = numbers.reduce((t, n) => t + n, 0); </script> <p>{numbers.join(' + ')} = {sum}</p> <button on:click={addNumber}> Add a number </button> <p> {obj.foo.bar} </p>
export
<script>
export let answer;
</script>
<p>The answer is {answer}</p>
父组件:
<script>
import Nested from './Nested.svelte';
</script>
<Nested answer={412122}/>
对比Vue:prop
=> export
{...xxx}
<script>
import Info from './Info.svelte';
const pkg = {
name: 'svelte',
version: 3,
speed: 'blazing',
website: 'https://svelte.dev'
};
</script>
<Info {...pkg}/>
{#if xxx}...{/if}
<script> let user = { loggedIn: false }; function toggle() { user.loggedIn = !user.loggedIn; } </script> {#if user.loggedIn} <button on:click={toggle}> Log out </button> {/if} {#if !user.loggedIn} <button on:click={toggle}> Log in </button> {/if}
对比vue:v-if
=>{#if xxx}...{/if}
{#if xxx}...{:else}...{/if}
<script> let user = { loggedIn: false }; function toggle() { user.loggedIn = !user.loggedIn; } </script> {#if user.loggedIn} <button on:click={toggle}> Log out </button> {:else} <button on:click={toggle}> Log in </button> {/if}
对比vue:v-if v-else
=>{#if xxx}...{:else}...{/if}
{#if xxx}...{:else if xxx}...{:else}...{/if}
<script>
let x = 7;
</script>
{#if x > 10}
<p>{x} is greater than 10</p>
{:else if 5 > x}
<p>{x} is less than 5</p>
{:else}
<p>{x} is between 5 and 10</p>
{/if}
{#each xxx as xx}...{/each}
<script> let cats = [ { id: 'J---aiyznGQ', name: 'Keyboard Cat' }, { id: 'z_AbfPXTKms', name: 'Maru' }, { id: 'OUtn3pvWmpg', name: 'Henri The Existential Cat' } ]; </script> <h1>The Famous Cats of YouTube</h1> <ul> <!-- open each block --> {#each cats as cat, i} <li><a target="_blank" href="https://www.youtube.com/watch?v={cat.id}"> {i + 1}:{cat.name} </a></li> {/each} <!-- close each block --> </ul>
对比vue:v-for
=>{#each xxx as xx}...{/each}
{#each xxx as xx(key-xxx)}...{/each}
<script> import Thing from './Thing.svelte'; let things = [ { id: 1, color: '#0d0887' }, { id: 2, color: '#6a00a8' }, { id: 3, color: '#b12a90' }, { id: 4, color: '#e16462' }, { id: 5, color: '#fca636' } ]; function handleClick() { things = things.slice(1); } </script> <button on:click={handleClick}> Remove first thing </button> {#each things as thing (thing.id)} <Thing current={thing.color}/> <span>{thing.id}</span> {/each}
对比vue::key="key-xxx"
=>{#each xxx as xx(key-xxx)}...{/each}
{#await xxx}...{:then xxx}...{:catch error}...{/await}
<script> let promise = getRandomNumber(); async function getRandomNumber() { const res = await fetch(`tutorial/random-number`); const text = await res.text(); if (res.ok) { return text; } else { throw new Error(text); } } function handleClick() { promise = getRandomNumber(); } </script> <button on:click={handleClick}> generate random number </button> <!-- 方法一:全面 --> {#await promise} <p>waiting</p> {:then value} <p> {value} </p> {:catch error} <p> {error} </p> {/await} <!-- 方法二:省略 --> {#await promise then value} <p>{value}</p> {/await}
on:xxx={xxx}
<script> let m = { x: 0, y: 0 }; function handleMousemove(event) { m.x = event.clientX; m.y = event.clientY; } </script> <style> div { width: 100%; height: 100%; } </style> <div on:mousemove={handleMousemove}> The mouse position is {m.x} x {m.y} </div>
<script> let m = { x: 0, y: 0 }; function handleMousemove(event) { m.x = event.clientX; m.y = event.clientY; } </script> <style> div { width: 100%; height: 100%; } </style> <div on:mousemove={e => m = {x: e.clientX, y: e.clientY}}> The mouse position is {m.x} x {m.y} </div>
on:xxx|once
所有修饰符列表:
<script>
function handleClick() {
alert('clicked')
}
</script>
<button on:click|once={handleClick}>
Click me
</button>
on:click|once|capture={...}
<script>
import { createEventDispatcher } from 'svelte';
// setup code goes here
const dispatch = createEventDispatcher()
function sayHello() {
dispatch('message', {
text: 'Hello zoey'
})
}
</script>
<button on:click={sayHello}>
Click to say hello zoey
</button>
<script>
import Inner from './Inner.svelte';
function handleMessage(event) {
alert(event.detail.text);
}
</script>
<Inner on:message={handleMessage}/>
on:message
、on:click
message 没有赋予特定的值得情况下意味着转发所有massage事件:
<script>
import Inner from './Inner.svelte';
</script>
<Inner on:message/>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。