svelte教程(8)stores
有时,您将需要多个不相关的组件或常规的JavaScript模块访问这些值。
在Svelte,我们通过store来做到这一点。store只是一种对象,该对象具有一种subscribe方法,该方法允许在store的value发生变化时通知订阅过的组件。
可写 store
通过 writable 方法可以创建一个可写store,传入两个参数value, start。
- value:初始值
- start:获得第一个订阅者时调用,拥有一个参数为set的回调。可以返回一个stop方法,该方法在最后一个订阅者退订时执行。
import { writable } from 'svelte/store'; export const count = writable(0,(set)=>{ console.log('subscribe count') set(100) return ()=>{ console.log('clear count') } });
可读store拥有三个方法:update 、set、subscribe。
- set: 设置value值。
- update: 更新value值,接受一个参数为value的方法,return一个新的value值。
function update(fn) { set(fn(value)); }
- subscribe: 订阅该store,接受一个参数为value的方法,用于获得value值并进行处理。返回值为一个退订方法,执行该方法完成退订。
<script> import { onDestroy } from "svelte"; import { count } from "../stores"; let count_value; const unsubscribe = count.subscribe(value => { count_value = value; }); onDestroy(unsubscribe()); </script> <h1>The count is {count_value}</h1> <button on:click={() => { count.update(c => c - 1); }}> - </button> <button on:click={() => { count.update(c => c + 1); }}> + </button> <button on:click={() => { count.set(0); }}> reset </button>
自动订阅
使用$进行自动订阅,自动订阅的store将在组件销毁时自动调用停止订阅方法。
<script> import { onDestroy } from "svelte"; import { count } from "../stores"; </script> <h1>The count is {$count}</h1> <button on:click={() => { count.update(c => c - 1); }}> - </button> <button on:click={() => { count.update(c => c + 1); }}> + </button> <button on:click={() => { count.set(0); }}> reset </button>
只读store
只读store就是没有暴露update、set方法的可写store。
import { readable } from 'svelte/store'; export const time = readable(new Date(), function start(set) { const interval = setInterval(() => { set(new Date()); }, 1000); return function stop() { clearInterval(interval); }; });
<script> import { time } from '../stores'; const formatter = new Intl.DateTimeFormat('en', { hour12: true, hour: 'numeric', minute: '2-digit', second: '2-digit' }); </script> <h1>The time is {formatter.format($time)}</h1>
派生store
您可以使用创建一个store,并且这个store基于其他一个或者多个store,可以使用派生store。
export const elapsed = derived( time, $time => Math.round(($time - start) / 1000) );
derived接受三个参数:
- stores:可以为一个store对象,或者为一个数组。
- fn:接受一个方法包含两个参数values,set。如果stores为数组,values也为数组,如果stores为store对象,values为改store的value。如果没有set参数,派生store的value为fn的返回值,如果包含set参数可以使用set方法指定value值。
- initial_value:初始值(异步时使用)
绑定 store
如果store是可写的(即它具有set方法),则可以绑定其值,就像可以绑定到本地组件一样。
也可以通过直接赋值将value写进store。
<input bind:value={$count}> <button on:click="{() => $count += 1}"> + 1 </button>
本教程的所有代码均上传到github有需要的同学可以参考 https://github.com/sullay/svelte-learn。
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
5个点彻底搞清楚SpringBoot注解
作者:张伯毅 一、注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan、@Configuration和@EnableAutoConfiguration注解。其中@ComponentScan让spring Boot扫描到Configuration类并把它加入到程序上下文。@Configuration 等同于spring的XML配置文件;使用Java代码可以检查类型安全。@EnableAutoConfiguration 自动配置。@ComponentScan 组件扫描,可自动发现和装配一些Bean。@Component可配合CommandLineRunner使用,在程序启动后执行一些基础任务。@RestController注解是@Controller和@ResponseBody的合集,表示这是个控制器bean,并且是将函数的返回值直 接填入HTTP响应体中,是REST风格的控制器。@Autowired自动导入。@PathVariable获取参数。@JsonBackReference解决嵌套外链问题。@RepositoryRes...
- 下一篇
Python使用数字与字符串的技巧
1.少写数字字面量 “数字字面量(integer literal)” 是指那些直接出现在代码里的数字。它们分布在代码里的各个角落,比如代码 del users[0] 里的 0 就是一个数字字面量。它们简单、实用,每个人每天都在写。但是,当你的代码里不断重复出现一些特定字面量时,你的“代码质量告警灯”就应该亮起黄灯 举个例子,假如你刚加入一家心仪已久的新公司,同事转交给你的项目里有这么一个函数: def mark_trip_as_featured(trip): """将某个旅程添加到推荐栏目 """ **if** trip.source== 11: do_some_thing(trip) elif trip.source== 12: do_some_other_thing(trip) **return** 这个函数做了什么事?你努力想搞懂它的意思,不过 trip.source == 11 是什么情况?那 == 12 呢?这两行代码很简单,没有用到任何魔法特性。但初次接触代码的你可能需要花费一整个下午,才能弄懂它们的含义。 问题就出在那几个数字字面量上。 最初写下这个函数的人,可能是在公司...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS6,CentOS7官方镜像安装Oracle11G
- Red5直播服务器,属于Java语言的直播服务器
- Hadoop3单机部署,实现最简伪集群
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS7安装Docker,走上虚拟化容器引擎之路
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- SpringBoot2整合Redis,开启缓存,提高访问速度
- CentOS6,7,8上安装Nginx,支持https2.0的开启