跳至主要內容

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

用於訂閱的 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

用於更新和訂閱的 Writable 介面。

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

設定值並通知訂閱者。

update(this: void, updater: Updater<T>): void;
  • updater 回呼

使用回呼更新值並通知訂閱者。

在 GitHub 上編輯此頁面

上一頁 下一頁