Skip to main content

只要一个对象正确地实现了 subscribe 方法,那它就是一个 store,除此之外,没有任何要求。因此要创建特定领域逻辑的自定义存储是很简单的。

比如说,我们之前例子中的 count 这个 store 可以包含 incrementdecrementreset 方法而避免暴露 setupdate

stores.js
function createCount() {
	const { subscribe, set, update } = writable(0);

	return {
		subscribe,
		increment: () => update((n) => n + 1),
		decrement: () => update((n) => n - 1),
		reset: () => set(0)
	};
}

Next: 存储绑定

1
2
3
4
5
6
7
8
9
10
<script>
	import { count } from './stores.js';
</script>
 
<h1>The count is {$count}</h1>
 
<button on:click={count.increment}>+</button>
<button on:click={count.decrement}>-</button>
<button on:click={count.reset}>reset</button>
 
initialising