Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getCurrentInstance type not correct #12596

Closed
iceprosurface opened this issue Jul 1, 2022 · 10 comments
Closed

getCurrentInstance type not correct #12596

iceprosurface opened this issue Jul 1, 2022 · 10 comments

Comments

@iceprosurface
Copy link

iceprosurface commented Jul 1, 2022

Version

2.7.0

Steps to reproduce

vue2.7 getCurrentInstance type was not match vue3 & @vue/composition-api

export function getCurrentInstance(): { proxy: Component } | null {
  return currentInstance && { proxy: currentInstance }
}

see:

What is expected?

has correct type

What is actually happening?

not match

@Akryum
Copy link
Member

Akryum commented Jul 1, 2022

I guess you are not supposed to access the component internals :)

@iceprosurface
Copy link
Author

iceprosurface commented Jul 1, 2022

In vue3:

export const getCurrentInstance: () => ComponentInternalInstance | null = () =>
  currentInstance || currentRenderingInstance

in @vue/composition-api

let currentInstance: ComponentInternalInstance | null = null
// ....
export function getCurrentInstance() {
  return currentInstance
}

They both returned ComponentInternalInstance. So vue2.7 are missing the following important function & props:

  • state
    • data
    • props
    • slots
  • function
    • emit
    • update

@Akryum
Copy link
Member

Akryum commented Jul 1, 2022

They are on getCurrentInstance().proxy which is roughly equivalent to this. So you can do proxy.$props or proxy.$emit for example.

@iceprosurface
Copy link
Author

Yes, i can do this by myself. But it cannot be smoothly upgrade from @vue/composition-api.

Is there any plan to add support for those api ?(This seems to be easy to implement)

@uyarn
Copy link

uyarn commented Jul 1, 2022

same issue :(

@yyx990803
Copy link
Member

  1. @vue/composition-api's implementation is not the source of truth - 2.7 has intentional deviations from it.

  2. Only exposing proxy is intentional, because other properties on the internal instance is considered, well, internal. You should refactor any code that relies on anything other than proxy.

    This is necessary because relying on the internal instance properties when using Composition API is a sign that you are misusing it. It makes your code less reusable AND introduces risk of compatibility with future versions of Vue. We exposed getCurrentInstance mostly for official 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.

  3. Vue 3 will likely start showing warnings when you rely on properties other than proxy soon.

@ulissepress
Copy link

ulissepress commented Aug 9, 2022

@yyx990803 considering that this API is not public, how do you suggest to get the vue app instance (the one created with createApp() ) from any Vue component (created using <script setup>) ?

Currently I was using getCurrentInstance().appContext.app

@F0rsaken
Copy link

F0rsaken commented Sep 6, 2022

@yyx990803 I know it's a closed issue and I understand your point of view, and I actually agree with you, but I think it's a good think to leave getCurrentInstance in API docs, even just to mention it. Even if only advanced library authors need it, they still need to know about it.

@woahitsjc
Copy link

Does anyone know an alternative approach to get the access to the Vue instance (for v2.7)?

@jacekkarczmarczyk
Copy link

Vue instance can be accessed by Vue , same like you access it in your main.js. If you mean component instance then you can access it by getCurrentInstance() (or getCurrentInstance().proxy), not sure what would be use case for using the first one, in my apps I'm using only the second one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants