-
-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reapply "simple & robust useData() impl"
This reverts commit 631f674.
- Loading branch information
Showing
3 changed files
with
28 additions
and
3 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 |
---|---|---|
@@ -1,10 +1,19 @@ | ||
// https://vike.dev/useData | ||
export { useData } | ||
export { setData } | ||
|
||
import { usePageContext } from './usePageContext' | ||
import { inject } from 'vue' | ||
import type { App } from 'vue' | ||
|
||
const key = Symbol() | ||
|
||
/** https://vike.dev/useData */ | ||
function useData<Data>(): Data { | ||
const { data } = usePageContext() | ||
const data = inject(key) | ||
if (!data) throw new Error('setData() not called') | ||
return data as any | ||
} | ||
|
||
function setData(app: App, data: unknown): void { | ||
app.provide(key, data) | ||
} |
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