-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add composable for core ICIJ/datashare#1528
- Loading branch information
Showing
1 changed file
with
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { getCurrentInstance } from 'vue' | ||
|
||
export function useCore() { | ||
// `getCurrentInstance` is a Vue Composition API function that gives us access to the current component instance. | ||
// This is essential because we need the instance to access global properties like `$core` and `$toast`. | ||
const instance = getCurrentInstance() | ||
|
||
// If `getCurrentInstance` is called outside of a Vue component setup function, it will return `null`. | ||
// In this case, we throw an error to alert the developer that `useCore` should only be used inside a setup function. | ||
if (!instance) { | ||
throw new Error('useCore must be called within a Vue component setup function.') | ||
} | ||
|
||
// The `instance` object has a `proxy` property, which is the component's public instance. | ||
// This `proxy` allows us to access properties like `$core` and `$toast` that are globally provided by the "core" plugin. | ||
const { proxy } = instance | ||
|
||
// We return an object with the global `$core` and `$toast` properties. | ||
// These properties can then be destructured and used in any component that calls `useCore`. | ||
return { | ||
// `proxy.$core` gives us access to the global `$core` object provided by the "core" plugin. | ||
core: proxy.$core, | ||
// `proxy.$toast` gives us access to the global `$toast` object provided by the "core" plugin. | ||
toast: proxy.$toast | ||
} | ||
} |