svelte/store
import {
function derived<S extends Stores, T>(stores: S, fn: (values: StoresValues<S>, set: (value: T) => void, update: (fn: Updater<T>) => void) => Unsubscriber | void, initial_value?: T | undefined): Readable<T> (+1 overload)
Derived value store by synchronizing one or more readable stores and
applying an aggregation function over its input values.
derived,
function fromStore<V>(store: Writable<V>): {
current: V;
} (+1 overload)
fromStore,
function get<T>(store: Readable<T>): T
Get the current value from a store by subscribing and immediately unsubscribing.
get,
function readable<T>(value?: T | undefined, start?: StartStopNotifier<T> | undefined): Readable<T>
Creates a Readable
store that allows reading by subscription.
readable,
function readonly<T>(store: Readable<T>): Readable<T>
Takes a store and returns a new one derived from the old one that is readable.
readonly,
function toStore<V>(get: () => V, set: (v: V) => void): Writable<V> (+1 overload)
toStore,
function writable<T>(value?: T | undefined, start?: StartStopNotifier<T> | undefined): Writable<T>
Create a Writable
store that allows both updating and reading by subscription.
writable
} from 'svelte/store';
derived
通过同步一个或多个可读存储并对其输入值应用聚合函数来派生值存储。
¥Derived value store by synchronizing one or more readable stores and applying an aggregation function over its input values.
function derived<S extends Stores, T>(
stores: S,
fn: (
values: StoresValues<S>,
set: (value: T) => void,
update: (fn: Updater<T>) => void
) => Unsubscriber | void,
initial_value?: T | undefined
): Readable<T>;
function derived<S extends Stores, T>(
stores: S,
fn: (values: StoresValues<S>) => T,
initial_value?: T | undefined
): Readable<T>;
fromStore
function fromStore<V>(store: Writable<V>): {
current: V;
};
function fromStore<V>(store: Readable<V>): {
readonly current: V;
};
get
通过订阅并立即取消订阅从存储获取当前值。
¥Get the current value from a store by subscribing and immediately unsubscribing.
function get<T>(store: Readable<T>): T;
readable
创建一个允许通过订阅阅读的 Readable
存储。
¥Creates a Readable
store that allows reading by subscription.
function readable<T>(
value?: T | undefined,
start?: StartStopNotifier<T> | undefined
): Readable<T>;
readonly
获取一个存储并返回一个从旧存储派生的可读新存储。
¥Takes a store and returns a new one derived from the old one that is readable.
function readonly<T>(store: Readable<T>): Readable<T>;
toStore
function toStore<V>(
get: () => V,
set: (v: V) => void
): Writable<V>;
function toStore<V>(get: () => V): Readable<V>;
writable
创建一个允许通过订阅更新和读取的 Writable
存储。
¥Create a Writable
store that allows both updating and reading by subscription.
function writable<T>(
value?: T | undefined,
start?: StartStopNotifier<T> | undefined
): Writable<T>;
可读(Readable)
¥Readable
可读的订阅界面。
¥Readable interface for subscribing.
interface Readable<T> {…}
subscribe(this: void, run: Subscriber<T>, invalidate?: () => void): Unsubscriber;
run
订阅回调¥
run
subscription callbackinvalidate
清理回调¥
invalidate
cleanup callback
订阅值更改。
¥Subscribe on value changes.
StartStopNotifier
启动和停止通知回调。当第一个订阅者订阅时,将调用此函数。
¥Start and stop notification callbacks. This function is called when the first subscriber subscribes.
type StartStopNotifier<T> = (
set: (value: T) => void,
update: (fn: Updater<T>) => void
) => void | (() => void);
订阅者(Subscriber)
¥Subscriber
回调以通知值更新。
¥Callback to inform of a value updates.
type Subscriber<T> = (value: T) => void;
取消订阅者(Unsubscriber)
¥Unsubscriber
取消订阅值更新。
¥Unsubscribes from value updates.
type Unsubscriber = () => void;
更新程序(Updater)
¥Updater
回调以更新值。
¥Callback to update a value.
type Updater<T> = (value: T) => T;
可写(Writable)
¥Writable
可写入接口,用于更新和订阅。
¥Writable interface for both updating and subscribing.
interface Writable<T> extends Readable<T> {…}
set(this: void, value: T): void;
要设置的
value
¥
value
to set
设置值并通知订阅者。
¥Set value and inform subscribers.
update(this: void, updater: Updater<T>): void;
updater
回调¥
updater
callback
使用回调更新值并通知订阅者。
¥Update value using callback and inform subscribers.