Skip to main content

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.

@paramvalue initial value
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.

@paramstore - store to make readonly
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.

@paramvalue initial value
writable
} from 'svelte/store';

derived

通过同步一个或多个可读 store 并对其输入值应用聚合函数来创建派生值 store。

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

通过订阅并立即取消订阅来获取 store 的当前值。

function get<T>(store: Readable<T>): T;

readable

创建一个允许通过订阅进行读取的 Readable store。

function readable<T>(
	value?: T | undefined,
	start?: StartStopNotifier<T> | undefined
): Readable<T>;

readonly

接受一个 store 并返回一个从旧 store 派生的新的只读 store 。

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 store 。

function writable<T>(
	value?: T | undefined,
	start?: StartStopNotifier<T> | undefined
): Writable<T>;

Readable

用于订阅的可读接口。

interface Readable<T> {}
subscribe(this: void, run: Subscriber<T>, invalidate?: () => void): Unsubscriber;
  • run 订阅回调
  • invalidate 清理回调

订阅值的变化。

StartStopNotifier

启动和停止通知回调。当第一个订阅者订阅时调用此函数。

type StartStopNotifier<T> = (
	set: (value: T) => void,
	update: (fn: Updater<T>) => void
) => void | (() => void);

Subscriber

通知值更新的回调。

type Subscriber<T> = (value: T) => void;

Unsubscriber

取消订阅值更新。

type Unsubscriber = () => void;

Updater

更新值的回调。

type Updater<T> = (value: T) => T;

Writable

用于更新和订阅的可写接口。

interface Writable<T> extends Readable<T> {}
set(this: void, value: T): void;
  • value 要设置的值

设置值并通知订阅者。

update(this: void, updater: Updater<T>): void;
  • updater 回调函数

使用回调更新值并通知订阅者。

在 GitHub 编辑此页面

上一页 下一页