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. [知乎]这可能是最全面的龙芯3A3000处理器评测

    这可能是最全面的龙芯3A3000处理器评测 第一千零一个人   已关注 蓬岸 Dr.Quest . https://zhuanlan.zhihu.com/p/50716952 这里面链接很全. 立党 ...

  2. - RabbitMQ - 0 - 介绍、linux 和windows安装

    目录 一. 介绍 二.windows安装erlang和rabbitMQ 三.Linux安装erlang和RabbitMQ 一. 介绍 rabbitMQ 是基于 erlang 语言开发的, 为了使用 r ...

  3. (六)mybatis 全局配置文件讲解

    目录 properties (属性) settings 全局参数配置 typeAliases 别名设置 typeHandlers 类型处理器 mapper (映射器) 细节 properties (属 ...

  4. Java:关于子类继承父类接口时,由于权限没有设定的更广,出错的一个小tip

    今天在写笔记的时候,写的地方出现了小叉叉错号. 发现问题: 这里出错了!原因是因为在子类覆写父类的方法的时候,权限不能开的比父类更低! 加了public后,纠错成功. 由于接口类型下的方法默认都是pu ...

  5. 网站页面顶部出现空白行&#65279字符的原因以及完美解决办法

    转自个人博客:https://www.hurbai.com 有时候网页头部会出现一个空白行,查看源码发现body开头初有一个非法字符 // 如果是Windows系统,修改为:$WIN = 1; $W ...

  6. 笔记-4:python组合数据类型

    1.字符串(str) 字符串是字符的序列表示, 根据字符串的内容多少分为单行字符串和多行字符串. 单行字符串可以由一对单引号(') 或双引号(")作为边界来表示, 单引号和双引号作用相同. ...

  7. ~ android与ios的区别

    Oracle与Mysql的区别 项目类别 android ios 应用上 可以使用常用的android模拟器,来模拟各种android设备 只能直接使用iphone或ipad进行测试 开发语言 基于L ...

  8. Android Studio中怎么使用DDMS工具

    随着android studio的广泛使用,开发人员对相关工具的使用需求更加凸显.昨天在一个android studio教程网站上,看到一篇有关DDMS工具使用的相关知识,感觉很不错,分享给大家,一起 ...

  9. python-day5(正式学习)

    格式化输出 符合某种输出规范的print函数的应用 第一种方式 使用占位符.漫威里有个人叫斯塔克,他平时站在人堆里(print函数引号内的内容)我们看不出来和其他人有什么异常(print的终端显示), ...

  10. pb相关小技巧或用法

    1.动态post window lwlw = w_main lw.dynamic post event ue_all(ls_no,ls_table) 2.打开隐藏窗口 IF NOT IsValid(w ...