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. AndroidStudio布局编辑器强制刷新布局界面

    用AndroidStudio布局编辑器编辑界面的时候,在selector里调整按钮的颜色,调整后的颜色经常无法实时显示在布局编辑器里,每次都重新运行程序查看界面又非常麻烦和低效,可以用以下方法解决: ...

  2. SQL SERVER导入EXCEL文件:无法创建链接服务器 "(null)" 的 OLE DB 访问接口 "Microsoft.Ace.OLEDB.12.0" 的实例。

    [方法一] --开启导入功能    exec sp_configure 'show advanced options',1    reconfigure    exec sp_configure 'A ...

  3. 安装matplotlib,报错ERROR: Command errored out with exit status 1:

    使用pip install matplotlib 出现报错信息: 发现这行报错 : 我是在pycharm上安装的,可是提示我去安装 Microsoft Visual C++ ,然后去百度查了下,发现只 ...

  4. ThreadLocal,Lock的事儿

    ThreadLocal作用 防止线程间的干扰 public interface Sequence { int getNumber(); } public class ClientThread exte ...

  5. Java面试--类加载顺序

    类什么时候就行初始化: 1)创建类的实例,也就是new一个对象  2)访问某个类或接口的静态变量,或者对该静态变量赋值  3)调用类的静态方法  4)反射(Class.forName(“com.fan ...

  6. numpy数组常用计算

    在说numpy库数组的计算之前先来看一下numpy数组形状的知识: 创建一个数组之后,可以用shape来查看其形状,返回一个元组 例如:a = np.array([[1, 2, 3], [4, 5, ...

  7. LeetCode. 计数质数

    题目要求: 统计所有小于非负整数 n 的质数的数量. 示例i: 输入: 10 输出: 4 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 . 代码: class Soluti ...

  8. java 缓存

    外存: 也就是我们经常说的(CDEF盘的大小)外储存器是指除计算机内存及CPU缓存以外的储存器,此类储存器一般断电后仍然能保存数据.常见的外存储器有硬盘.软盘.光盘.U盘等,一般的软件都是安装在外存中 ...

  9. 以前面试 经常写这种 问掉的 copy 还是 =

    get的时候,生成的  那个对象赋值给aa 生成的对象在这条语句完  就析构了: https://blog.csdn.net/qq_31759205/article/details/80544468h ...

  10. IDEA中安装go插件,如何能够配置go SDK?

    最近在学习go语言,一个是因为区块链的技术热潮,另一个是接手的项目有用到go写多线程高并发,因此决定自学go. 第一个遇到的问题就是环境! 通过一个晚上的摸索,大概步骤如下: 在IDEA中先打开set ...