-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(projects): 添加provide、inject上下文示例
- Loading branch information
1 parent
1523c7b
commit a444731
Showing
3 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { ref } from 'vue'; | ||
import type { Ref } from 'vue'; | ||
import { useContext } from '@/hooks'; | ||
|
||
interface DemoContext { | ||
counts: Ref<number>; | ||
setCounts: (count: number) => void; | ||
} | ||
|
||
const { useProvide: useDemoProvider, useInject: useDemoInject } = useContext<DemoContext>(); | ||
|
||
export function useDemoContext() { | ||
const counts = ref(0); | ||
|
||
function setCounts(count: number) { | ||
counts.value = count; | ||
} | ||
|
||
const demoContext: DemoContext = { | ||
counts, | ||
setCounts | ||
}; | ||
|
||
function useProvider() { | ||
useDemoProvider(demoContext); | ||
} | ||
|
||
return { | ||
useProvider, | ||
useInject: useDemoInject | ||
}; | ||
} | ||
|
||
// 示例用法: A.vue为父组件, B.vue为子孙组件 C.vue为子孙组件 | ||
// A.vue | ||
// import { useDemoContext } from '@/context'; | ||
// const { useProvider } = useDemoContext(); | ||
// useProvider(); | ||
|
||
// B.vue 和 C.vue : 共享状态 counts | ||
// import { useDemoContext } from '@/context'; | ||
// const { useInject } = useDemoContext(); | ||
// const { counts, setCounts } = useInject(); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './demo'; |
a444731
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
soybean-admin – ./
soybean-admin-git-main-honghuangdc.vercel.app
soybean-admin-honghuangdc.vercel.app
soybean-admin.vercel.app
soybean.pro
www.soybean.pro