Every Svelte component has a lifecycle that starts when it is created, and ends when it is destroyed. There are a handful of functions that allow you to run code at key moments during that lifecycle.

The one you'll use most frequently is onMount, which runs after the component is first rendered to the DOM.

In this lesson we're going to learn how to use onMount to fetch and render data from Star Wars API.

Doc: https://svelte.dev/docs#onMount

<script>
import {onMount} from 'svelte';
let people = []
onMount(async () => {
const response = await fetch('https://swapi.co/api/people/');
const json = await response.json();
people = json.results; return () => console.log('Destroyed');
})
</script> <ul>
{#each people as {name, height, birth_year}}
<li>
<strong>{name}</strong>
(height: {height}cm, birth year: {birth_year})
</li>
{:else}
<p>loading...</p>
{/each}
</ul>

[Svelte 3] Use an onMount lifecycle method to fetch and render data in Svelte 3的更多相关文章

  1. Modified Least Square Method and Ransan Method to Fit Circle from Data

    In OpenCv, it only provide the function fitEllipse to fit Ellipse, but doesn't provide function to f ...

  2. Method 'ExecuteAsync' in type 'System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy' does not have an implementation

    一.错误信息 Entity Framework 6.0数据迁移:Add-Migration XXXX 命令发生错误 System.Reflection.TargetInvocationExceptio ...

  3. Method and apparatus for encoding data to be self-describing by storing tag records describing said data terminated by a self-referential record

    A computer-implemented method and apparatus in a computer system of processing data generated by a f ...

  4. [React] Stop Memory Leaks with componentWillUnmount Lifecycle Method in React

    In this lesson we'll take a stopwatch component we built in another lesson and identify and fix a me ...

  5. Method not found : Void System.Data.Objects.ObjectContextOptions.set_UseConsistentNullReferenceBehavior(Boolean)

    找不到方法:“Void System.Data.Objects.ObjectContextOptions.set_UseConsistentNullReferenceBehavior(Boolean) ...

  6. MATLAB读取写入文本数据最佳方法 | Best Method for Loading & Saving Text Data Using MATLAB

    MATLAB读取文件有很多方法.然而笔者在过去进行数据处理中,由于函数太多,相互混杂,与C#,Python等语言相比,反而认为读取文本数据比较麻烦.C#和Python等高级语言中,对于大部分的文本数据 ...

  7. Displaying Data in a Chart with ASP.NET Web Pages (Razor)

    This article explains how to use a chart to display data in an ASP.NET Web Pages (Razor) website by ...

  8. [Redux] Fetching Data on Route Change

    We will learn how to fire up an async request when the route changes. A mock server data: /** /api/i ...

  9. Cryptographic method and system

    The present invention relates to the field of security of electronic data and/or communications. In ...

随机推荐

  1. mysql在字符编辑窗口下怎么退出编辑界面?(mysql下的ctrl+c与\c)

    [1]SQL编辑 我们在SQL编辑的时候打错了,想要退出编辑重新输入,或者是不想写了. 如下图 (1)如果我们直接按ctrl+c中断,那么直接退出整个linux了,如上图 (2)我们可以使用\c,直接 ...

  2. 【转帖】sysbench使用教程(包含ubuntu自带的0.4版本、0.5版本和最新的1.0版本)

    sysbench使用教程(包含ubuntu自带的0.4版本.0.5版本和最新的1.0版本) 2018年08月10日 09:54:42 cxin917 阅读数 429   版权声明:本文为博主原创文章, ...

  3. 【IDEA插件】—— 代码量统计工具Statistic

    1.下载 1.打开idea设置界面,选择 plugins标签 2.搜索“Statistic”插件,点击 install 3.重启IDEA 2.使用 1.菜单栏中找到view 2.在下层目录中找到Sta ...

  4. LC 349. Intersection of Two Arrays

    题目描述 Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1, ...

  5. 封装PHP增删改查方法

    <?php class sqlModel{ public $db; public function __construct(){ try{ $dbms='mysql';//数据库类型 $dbNa ...

  6. Python第三方库资源

    [转载]Python第三方库资源   转自:https://weibo.com/ttarticle/p/show?id=2309404129469920071093 参考:https://github ...

  7. AlertManager 部署及使用

    熟悉了 Grafana 的报警功能,但是 Grafana 的报警功能目前还比较弱,只支持 Graph 的图表的报警.今天来给大家介绍一个功能更加强大的报警工具:AlertManager. 简介 之前我 ...

  8. html5手机网页开发,中文输入法下软键盘遮挡输入框

    安卓手机解决办法 微信UI框架weui中给出了解决方法:weui框架http://weui.github.io/weui/example.js // .container 设置了 overflow 属 ...

  9. MyBatis MyBatis Generator入门

    一.MGB功能简介 MyBatis Generator是一个代码生成工具. MBG是如何运行的呢?它会检查所连接到的数据库的一个或者多个table,然后生成可用来访问这些table的构建(Java代码 ...

  10. hdu 6058 思维

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=6058 分析题目的时候,由于枚举的区间很多,而第k大的值范围小,应该要想到去枚举第k大的值然后找到这个值对答 ...