From 637cfed4c1f5ada11aef550fd810e1cb381ea985 Mon Sep 17 00:00:00 2001 From: pikax Date: Sun, 4 Oct 2020 09:30:17 +0100 Subject: [PATCH 1/2] feat(api): composition-api `getCurrentInstance` --- src/api/composition-api.md | 55 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/api/composition-api.md b/src/api/composition-api.md index 2494474fe8..d7ba87a939 100644 --- a/src/api/composition-api.md +++ b/src/api/composition-api.md @@ -158,3 +158,58 @@ const foo = inject('foo') // string | undefined - **See also**: - [Provide / Inject](../guide/component-provide-inject.html) - [Composition API Provide / Inject](../guide/composition-api-provide-inject.html) + +## `getCurrentInstance` + +`getCurrentInstance` enables access to internal component intance usefull for advanced usages or for library creators. + +```ts +import { getCurrentIntance } from 'vue' + +const MyComponent = { + setup() { + const internalIntance = getCurrentInstance() + + internalInstance.appContext.config.globalProperties // access to globalProperties + } +} +``` + +`getCurrentInstance` **only** works during [setup](#setup) or [Lifecycle Hooks](#lifecycle-hooks) + +> When using outside of [setup](#setup) or [Lifecycle Hooks](#lifecycle-hooks), please call `getCurrentInstance()` on `setup` and use the instance instead. + +```ts +const MyComponent = { + setup() { + const internalIntance = getCurrentInstance() // works + + const id = useComponentId() // works + + const handleClick = () => { + getCurrentInstance() // doesn't work + useComponentId() // doesn't work + + internalIntance // works + } + + onMounted(() => { + getCurrentInstance() // works + }) + + return () => + h( + 'button', + { + onClick: handleClick + }, + `uid: ${id}` + ) + } +} + +// also works if called on a composable +function useComponentId() { + return getCurrentInstance().uid +} +``` From 4202b1610f20ed310868dabd0c021cade8d0ab97 Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Sun, 4 Oct 2020 10:53:43 +0100 Subject: [PATCH 2/2] Update src/api/composition-api.md Co-authored-by: Natalia Tepluhina --- src/api/composition-api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/composition-api.md b/src/api/composition-api.md index d7ba87a939..f6c9e6ba0b 100644 --- a/src/api/composition-api.md +++ b/src/api/composition-api.md @@ -161,7 +161,7 @@ const foo = inject('foo') // string | undefined ## `getCurrentInstance` -`getCurrentInstance` enables access to internal component intance usefull for advanced usages or for library creators. +`getCurrentInstance` enables access to an internal component instance useful for advanced usages or for library creators. ```ts import { getCurrentIntance } from 'vue'