<script>
let name = 'world';
</script>
<h1>Hello {name}!</h1>
// 控制属性
<script>
let src = 'tutorial/image.gif';
</script>
<img src={src} alt="test">
<img {src} alt>
// 使用html模板
<script>
let string = `this string contains some HTML!!!`;
</script>
<p>{string}</p>
//parent.svelte
<script>
import Child from 'Child.svelte'
</script>
<p>我是父组件</p>
<Child />
//child.svelte
<p>我是子组件</p>
<script>
let count = 0;
$: doubled = count * 2; //类比computed
$: watch(count); //类比watch
$: {
console.log(`the count is ${count}`);
alert(`I SAID THE COUNT IS ${count}`);
}
$: if (count >= 10) {
alert(`count is dangerously high!`);
count = 9;
}
function watch(val){
console.log(val)
}
function handleClick() {
count += 1;
}
</script>
<button on:click={handleClick}>
Clicked {count} {count === 1 ? 'time' : 'times'}
</button>
<p>{count} doubled is {doubled}</p>
//parent.svelte
<script>
import Nested from './Nested.svelte';
</script>
<Nested answer={42}/>
//child.svelte
<script>
export let answer;
</script>
<p>The answer is {answer}</p>
//默认属性值
<script>
export let answer = 'defaultValue';
</script>
//父中引用
<Nested/>
//对象属性
<Info {...pkg}/> //父组件
<script> // 子组件
export let name;
export let version;
export let speed;
export let website;
</script>
// 简单判断
{#if user.loggedIn}
<button on:click={toggle}>
Log out
</button>
{/if}
// 分支判断
{#if user.loggedIn}
<button on:click={toggle}>
Log out
</button>
{:else}
<button on:click={toggle}>
Log in
</button>
{/if}
// 多分支判断
{#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}
<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>
{#each cats as { id, name }, i}
<li><a target="_blank" href="https://www.youtube.com/watch?v={id}">
{i + 1}: {name}
</a></li>
{/each}
</ul>
// 绑定key
{#each things as thing (thing.id)}
<Thing current={thing.color}/>
{/each}
<script>
<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 number}
<p>The number is {number}</p>
{:catch error}
<p style="color: red">{error.message}</p>
{/await}
| 修饰符 | 作用 |
|---|---|
| preventDefault | 调用event.preventDefault() ,在运行处理程序之前调用。比如,对客户端表单处理有用 |
| stopPropagation | 调用event.stopPropagation(),阻止事件冒泡 |
| passive | 优化了对 touch/wheel 事件的滚动表现(Svelte 会在合适的地方自动添加滚动条) |
| capture | 在 capture 阶段而不是bubbling 阶段触发事件处理程序 |
| once | 运行一次事件处理程序后将其删除 |
| self | 仅当 event.target 是其本身时才执行 |
//parent.svelte
<script>
import Inner from './Inner.svelte';
function handleMessage(event) {
alert(event.detail.text);
}
</script>
<Inner on:message={handleMessage}/>
//child.svelte
<script>
import { createEventDispatcher } from 'svelte';
const dispatch = createEventDispatcher();
function sayHello() {
dispatch('message', {
text: 'Hello!'
});
}
</script>
<button on:click={sayHello}>
Click to say hello
</button>