18

I've seen references to getCurrentInstance() function on some old(?) documents and codes, but cannot find it in the current Vue 3 document.

Is getCurrentInstance() deprecated?

If so, what is the reason? (inject() considered enough?) If not, why can't I find it in the document?

1

4 Answers 4

43

getCurrentInstance() was removed from the Vue 3 docs because it's an internal API:

Because the instance is an internal instance that exposes non-public APIs. Anything you use from that instance can technically break between any release types, since they are not subject to semver constraints.


getCurrentInstance() was originally documented in 4-Oct-2020, but that was later removed in 31-Aug-2021 in a major refactoring of the Composition API docs by the creator of Vue (Evan You). Despite its removal from the docs, getCurrentInstance() still:

Given that it's an undocumented internal API, use it with caution.

Sign up to request clarification or add additional context in comments.

1 Comment

so how can I get $el?
5

https://github.com/vuejs/vue/issues/12596#issuecomment-1173269807

quoting the comment from evan,

getCurrentInstance is used mostly for official vue libraries that need additional internal access, not for userland application code. It was mistakenly documented in WIP v3 docs but is no longer considered a public API

Comments

1

The VueUse library extensively uses getComponentState() to detect if code is called within the component lifecycle.

For example:

https://github.com/vueuse/vueuse/blob/main/packages/shared/tryOnBeforeUnmount/index.ts

I'm going to go out on a limb and say,

"If it is good enough for the VueUse maestro antfu, it is good enough for me."

So although officially deprecated, it is still essential until Vue provides a method such as isInComponentLifecycle().

My recommendation is to only use it for this type of detection, and avoid accessing it's members if possible.

Comments

0

Also, it looks like this is what is used for exposing component "Methods" in Quasar from eg. QDialog.js:

  setup (props, { slots, emit, attrs }) {
    const vm = getCurrentInstance()
    ....
    Object.assign(vm.proxy, {
      // expose public methods
      focus, shake,
      ....
    });
    ....
  }

Not exactly an answer, sorry, but more a followup on why it is not 'really' an internal API

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.