Created
October 14, 2020 17:19
-
-
Save laiso/d400ffaee58b2d0194e262fc7ead1306 to your computer and use it in GitHub Desktop.
useCounterの中身。injectしてるからVueインスタンス経由でアクセスしないといけない。アプリケーションコードは上流でprovide()してコンポーネント間で状態管理する #CodePiece
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ref, inject, Ref } from 'vue'; | |
export const CounterKey = Symbol() | |
export type Counter = { | |
count: Ref | |
increment(): void | |
} | |
export function useCounter(initialValue: number): Counter { | |
const count = inject(CounterKey, ref(initialValue)); | |
return { | |
count, | |
increment(): void { | |
count.value +=1; | |
}, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment