美文网首页
Getting started with Svelte - Re

Getting started with Svelte - Re

作者: 游文影月志 | 来源:发表于2023-11-23 15:56 被阅读0次

Assignments

At the heart of Svelte is a powerful system of reactivity for keeping the DOM in sync with your application state — for example, in response to an event.

To demonstrate it, we first need to wire up an event handler. Inside the increment function, all we need to do is change the value of count:

<script>
    let count = 0;

    function increment() {
        count += 1;
    }
</script>

<button on:click={increment}>
    Clicked {count}
    {count === 1 ? 'time' : 'times'}
</button>

Declarations

Svelte automatically updates the DOM when your component's state changes. Often, some parts of a component's state need to be computed from other parts (such as a fullname derived from a firstname and a lastname), and recomputed whenever they change.

For these, we have reactive declarations. $: doubled = count * 2;

<script>
    let count = 0;
    $: doubled = count * 2;

    function increment() {
        count += 1;
    }
</script>

<button on:click={increment}>
    Clicked {count}
    {count === 1 ? 'time' : 'times'}
</button>

<p>{count} doubled is {doubled}</p>

If a reactive statement consists entirely of an assignment to an undeclared variable, Svelte will inject a let declaration on your behalf.

Note that reactive declarations and statements will run after other script code and before component markup is rendered.

Statement

We're not limited to declaring reactive values — we can also run arbitrary statements reactively. For example, we can log the value of count whenever it changes:

let count = 0;

$: console.log(`the count is ${count}`);

You can easily group statements together with a block:

$: {
    console.log(`the count is ${count}`);
    console.log(`this will also be logged whenever count changes`);
}

You can even put the $: in front of things like if blocks:

$: if (count >= 10) {
    alert('count is dangerously high!');
    count = 0;
}
<script>
    let count = 0;

    $: if (count >= 10) {
        alert('count is dangerously high!');
        count = 0;
    }

    function handleClick() {
        count += 1;
    }
</script>

<button on:click={handleClick}>
    Clicked {count}
    {count === 1 ? 'time' : 'times'}
</button>

Updating arrays and objects

Because Svelte's reactivity is triggered by assignments, using array methods like push and splice won't automatically cause updates.

One way to fix that is to add an assignment that would otherwise be redundant.

function addNumber() {
    numbers.push(numbers.length + 1);
    numbers = numbers;
}

Array.prototype.push()

But there's a more idiomatic solution:

function addNumber() {
    numbers = [...numbers, numbers.length + 1];
}

or

function addNumber() {
    numbers[numbers.length] = numbers.length + 1;
}

You can use similar patterns to replace pop, shift, unshift and splice.

Array.prototype.pop()

The pop() method of Array instances removes the last element from an array and returns that element. This method changes the length of the array.

let numbers = [1, 2, 3, 4];

numbers = [...numbers.slice(0, numbers.length - 1)];
// [1, 2, 3]

Array.prototype.shift()

The shift() method of Array instances removes the first element from an array and returns that removed element. This method changes the length of the array.

let numbers = [1, 2, 3, 4];

numbers = [...numbers.slice(1, numbers.length)];
// [2, 3, 4]

Array.prototype.unshift()

The unshift() method of Array instances adds the specified elements to the beginning of an array and returns the new length of the array.

let numbers = [1, 2, 3, 4];

numbers = [0, ...numbers];
// [0, 1, 2, 3, 4]

相关文章

  • 2021-05-05 It’s called SIMPLE

    Since we’re just getting started, we thought the best way...

  • React第一课

    学习地址:http://reactjs.cn/react/docs/getting-started.html Re...

  • LLVM入门参考资料

    Getting Started   - Getting Started with the LLVM System ...

  • Getting Started

    坑爹的我要开始系统学习web前端的所有知识了。两年前的实习算是基本入了个门,别的没学到,学了git这种版本控制工...

  • Getting Started

    Step-by-step guides for deploying your first app and mast...

  • Getting Started

    This project is for ideas sharing use. If you get any tho...

  • Getting Started

    webpack is a tool to build JavaScript modules in your app...

  • Getting Started

    好记性不如烂笔头,从今儿个开始,我所有的学习笔记或者工作中的“填坑”记录将记录在这里,顺便也就权当是分享了。如果对...

  • Getting Started

    Building Your First App Supporting Different Devices Buil...

  • 这3个行为,让你失去更多升职的机会

    The secret of getting ahead is getting started. – Mark Tw...

网友评论

      本文标题:Getting started with Svelte - Re

      本文链接:https://www.haomeiwen.com/subject/uyiwwdtx.html