svelte教程(7)生命周期

简介: onMountonMount将在组件首次呈现到DOM之后运行。除了onDestroy之外,生命周期函数不会在SSR期间运行,这意味着一旦组件挂在到DOM上,我们就可以避免在DOM构建之前获取到应在DOM构建以后加载的数据。

onMount

onMount将在组件首次呈现到DOM之后运行。除了onDestroy之外,生命周期函数不会在SSR期间运行,这意味着一旦组件挂在到DOM上,我们就可以避免在DOM构建之前获取到应在DOM构建以后加载的数据。

必须在逐渐初始化时调用生命周期函数,不能再setTimeout中调用。

如果onMount返回一个函数,则在销毁组件时将调用该函数。

<script>
    import { onMount } from 'svelte';

    let photos = [];

    onMount(async () => {
        const res = await fetch(`https://jsonplaceholder.typicode.com/photos?_limit=20`);
        photos = await res.json();
    });
</script>

<style>
    .photos {
        width: 100%;
        display: grid;
        grid-template-columns: repeat(5, 1fr);
        grid-gap: 8px;
    }

    figure, img {
        width: 100%;
        margin: 0;
    }
</style>

<h1>Photo album</h1>

<div class="photos">
    {#each photos as photo}
        <figure>
            <img src={photo.thumbnailUrl} alt={photo.title}>
            <figcaption>{photo.title}</figcaption>
        </figure>
    {:else}
        <!-- this block renders when photos.length === 0 -->
        <p>loading...</p>
    {/each}
</div>

通过测试发现onMount的回调函数执行顺序在onDestroy之后。并且返回不能是promise对象,也就意味着如果使用async关键字将不会在组件销毁时执行。

<script>
  import { onMount, onDestroy } from "svelte";

  let photos = [];
  onMount(() => {
    fetch(`https://jsonplaceholder.typicode.com/photos?_limit=20`)
      .then(res => {
        return res.json();
      })
      .then(res => {
        photos = res;
      });
    return () => {
      console.log("onMount end2");
    };
  });
  onDestroy(() => {
    console.log("onDestroy2");
  });
</script>

<style>
  .photos {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    grid-gap: 8px;
  }

  figure,
  img {
    width: 100%;
    margin: 0;
  }
</style>

<a href="/">Photo album</a>

<div class="photos">
  {#each photos as photo}
    <figure>
      <img src={photo.thumbnailUrl} alt={photo.title} />
      <figcaption>{photo.title}</figcaption>
    </figure>
  {:else}
    <!-- this block renders when photos.length === 0 -->
    <p>loading...</p>
  {/each}
</div>

onDestory

当销毁组件时调用。

<script>
  import { onDestroy } from "svelte";
  let seconds = 0;
  const interval = setInterval(() => (seconds += 1), 1000);
  onDestroy(() => clearInterval(interval));
</script>

<p>The page has been open for {seconds} {seconds === 1 ? 'second' : 'seconds'}</p>

beforeUpdate 、afterUpdate

beforeUpdate 在DOM更新前运行,afterUpdate 在DOM更新后运行。

<script>
    import Eliza from 'elizabot';
    import { beforeUpdate, afterUpdate } from 'svelte';

    let div;
    let autoscroll;

    beforeUpdate(() => {
        autoscroll = div && (div.offsetHeight + div.scrollTop) > (div.scrollHeight - 20);
    });

    afterUpdate(() => {
        if (autoscroll) div.scrollTo(0, div.scrollHeight);
    });

    const eliza = new Eliza();

    let comments = [
        { author: 'eliza', text: eliza.getInitial() }
    ];

    function handleKeydown(event) {
        if (event.which === 13) {
            const text = event.target.value;
            if (!text) return;

            comments = comments.concat({
                author: 'user',
                text
            });

            event.target.value = '';

            const reply = eliza.transform(text);

            setTimeout(() => {
                comments = comments.concat({
                    author: 'eliza',
                    text: '...',
                    placeholder: true
                });

                setTimeout(() => {
                    comments = comments.filter(comment => !comment.placeholder).concat({
                        author: 'eliza',
                        text: reply
                    });
                }, 500 + Math.random() * 500);
            }, 200 + Math.random() * 200);
        }
    }
</script>

<style>
    .chat {
        display: flex;
        flex-direction: column;
        height: 500px;
        max-width: 320px;
    margin: 0 auto;
    }

    .scrollable {
        flex: 1 1 auto;
        border-top: 1px solid #eee;
        margin: 0 0 0.5em 0;
        overflow-y: auto;
    }

    article {
        margin: 0.5em 0;
    }
  .eliza{
    text-align: left;
  }
    .user {
        text-align: right;
    }

    span {
        padding: 0.5em 1em;
        display: inline-block;
    }

    .eliza span {
        background-color: #eee;
        border-radius: 1em 1em 1em 0;
    }

    .user span {
        background-color: #0074D9;
        color: white;
        border-radius: 1em 1em 0 1em;
    }
</style>

<div class="chat">
    <h1>Eliza</h1>

    <div class="scrollable" bind:this={div}>
        {#each comments as comment}
            <article class={comment.author}>
                <span>{comment.text}</span>
            </article>
        {/each}
    </div>

    <input on:keydown={handleKeydown}>
</div>

tick

tick不同于其他生命周期,因为您可以随时调用它,而不仅是组件首次初始化时。它返回一个promise,该promise在任何pending状态的promise状态更改并应用于DOM时立刻执行resolve。如果没有pending状态的promise时,则立即执行resolve。

当您在Svelte中更改组件状态时,它不会立即更新DOM。而是等到下一个微任务,看看是否还有其他需要应用的更改,包括在其他组件中。这样做避免了不必要的工作,并使浏览器可以更有效地对事物进行批处理。

下面例子中使用tab切换大小写,如不过不适用tick,由于text 更改后,dom并没有马上更新,所以立刻设置光标位置然后dom更新后失效。使用tick可以解决这个问题。

<script>
    import { tick } from 'svelte';

    let text = `Select some text and hit the tab key to toggle uppercase`;

    async function handleKeydown(event) {
        if (event.which !== 9) return;

        event.preventDefault();

        const { selectionStart, selectionEnd, value } = this;
        const selection = value.slice(selectionStart, selectionEnd);

        const replacement = /[a-z]/.test(selection)
            ? selection.toUpperCase()
            : selection.toLowerCase();

        text = (
            value.slice(0, selectionStart) +
            replacement +
            value.slice(selectionEnd)
        );

        await tick();
        this.selectionStart = selectionStart;
        this.selectionEnd = selectionEnd;
    }
</script>

<style>
    textarea {
        width: 100%;
        height: 200px;
    }
</style>

<textarea value={text} on:keydown={handleKeydown}></textarea>

本教程的所有代码均上传到github有需要的同学可以参考 https://github.com/sullay/svelte-learn

目录
相关文章
|
1月前
|
设计模式 JavaScript 前端开发
探秘 Vue2 的 8 大生命周期
Vue.js 是一种用于构建用户界面的 JavaScript 框架。它采用了 MVVM(Model-View-ViewModel)设计模式,使得开发者能够轻松地将数据模型与 UI 分离。在 Vue.js 中,一个组件的生命周期是指从创建到销毁的过程,包括一系列的事件和钩子函数。了解这些生命周期钩子函数以及它们在何时被调用,可以帮助我们更好地管理和优化组件的性能。
|
3月前
|
前端开发 JavaScript 测试技术
第十三章 React生命周期(新)
第十三章 React生命周期(新)
|
4月前
|
JavaScript
vue中的生命周期有什么,怎么用
vue中的生命周期有什么,怎么用
14 0
|
11天前
|
JavaScript 前端开发
【前端学习】—Vue生命周期(十七)
【前端学习】—Vue生命周期(十七)
|
6月前
|
存储 开发框架 JavaScript
开发项目的前后端分离,学习Vue的生命周期
开发项目的前后端分离,学习Vue的生命周期
22 0
|
9月前
|
JavaScript 前端开发 开发者
一篇读懂React、vue框架的生命周期函数
一篇读懂React、vue框架的生命周期函数
57 0
|
前端开发
前端学习案例5-react中的生命周期
前端学习案例5-react中的生命周期
52 0
前端学习案例5-react中的生命周期
|
前端开发 JavaScript
简单手写实现React的基本生命周期
简单手写实现React的基本生命周期
108 0
|
存储 前端开发 JavaScript
React对于生命周期的深入研究
React对于生命周期的深入研究
React对于生命周期的深入研究
|
移动开发
ReactNative入门教程-组件生命周期函数
ReactNative入门教程-组件生命周期函数
79 0
ReactNative入门教程-组件生命周期函数