@@ -490,7 +490,7 @@ mouse.x === x.value // true
490490
491491The ` watch ` and ` computed ` will stop themselves on components unmounted. <br >We'd recommend following the same pattern for your custom composable functions.
492492
493- <div >
493+ <div v-click >
494494
495495``` ts{monaco}
496496import { onUnmounted } from 'vue'
@@ -508,7 +508,7 @@ export function useEventListener(target: EventTarget, name: string, fn: any) {
508508
509509</div >
510510
511- <div class =" abs-b mx-14 my-12 " >
511+ <div v-click class =" abs-b mx-14 my-12 " >
512512<VueUse name =" useEventListener " />
513513</div >
514514
@@ -539,12 +539,6 @@ const scope = effectScope(() => {
539539stop (scope )
540540```
541541
542- ---
543- layout: center
544- ---
545-
546- # Tips
547-
548542------
549543
550544# Template Ref <MarkerTips />
@@ -594,6 +588,7 @@ watch(element, (el) => {
594588------
595589
596590# Typed Provide / Inject <MarkerCore />
591+
597592Use the ` InjectionKey<T> ` helper from Vue to share types across context.
598593
599594<div >
@@ -615,6 +610,7 @@ export const injectKeyUser: InjectionKey<UserInfo> = Symbol()
615610------
616611
617612# Typed Provide / Inject <MarkerCore />
613+
618614Import the key from the same module for ` provide ` and ` inject ` .
619615
620616<div class =" grid grid-cols-2 gap-4 " >
@@ -667,39 +663,100 @@ export const injectKeyUser: InjectionKey<UserInfo> = Symbol()
667663
668664------
669665
670- # App Level Singleton
666+ # Shared State <MarkerPattern />
667+
668+ By the nature of Composition API, states can be created and used independently.
671669
672670<div class =" grid grid-cols-2 gap-4 " >
673671
672+ <v-click >
673+
674+ ``` ts
675+ // shared.ts
676+ import { reactive } from ' vue'
677+
678+ export const state = reactive ({
679+ foo: 1 ,
680+ bar: ' Hello'
681+ })
682+ ```
683+
684+ </v-click >
685+
686+ <div >
687+ <v-clicks >
688+
689+ ``` ts
690+ // A.vue
691+ import { state } from ' ./shared.ts'
692+
693+ state .foo += 1
694+ ```
695+
674696``` ts
675- export const keyMyTool: InjectionKey <MyTool > = Symbol ()
697+ // B.vue
698+ import { state } from ' ./shared.ts'
699+
700+ console .log (state .foo ) // 2
701+ ```
702+
703+ </v-clicks >
704+ </div >
705+ </div >
706+
707+ <h3 v-click class =" opacity-100 " >⚠️ But it's not SSR compatible!</h3 >
708+
709+ ------
710+
711+ # Shared State (SSR friendly) <MarkerPattern />
712+
713+ <div class =" grid grid-cols-[max-content,1fr] gap-4 " >
714+
715+ <v-click >
676716
677- export function createMyTool() {
717+ ``` ts
718+ export const myStateKey: InjectionKey <MyState > = Symbol ()
719+
720+ export function createMyState() {
678721 const state = {
679722 /* ... */
680723 }
681724
682725 return {
683726 install(app : App ) {
684- app .provide (keyMyTool , state )
727+ app .provide (myStateKey , state )
685728 }
686729 }
687730}
688731
689- export function useMyTool (): MyTool {
690- return inject (keyMyTool )!
732+ export function useMyState (): MyState {
733+ return inject (myStateKey )!
691734}
692735```
693736
737+ </v-click >
738+
739+ <div >
740+ <v-clicks >
741+
694742``` ts
743+ // main.ts
695744const App = createApp (App )
696745
697- app .use (createMyTool ())
746+ app .use (createMyState ())
698747```
699748
749+ ``` ts
750+ // A.vue
751+
752+ // use everywhere in your app
753+ const state = useMyState ()
754+ ```
755+
756+ </v-clicks >
700757</div >
701758
702- > TODO:
759+ </ div >
703760
704761------
705762
0 commit comments