Home > Back-end >  How to get the component name from a Proxy in Vue
How to get the component name from a Proxy in Vue

Time:08-24

I'm implementing one rrorCaptured in a Vue component. It has 3 parameters (error: unknown, instance: ComponentPublicInstance, info: string). instance actually ends up being a Proxy. Since the error bubbles up to the component that I'm implementing the one rrorCaptured in, I am trying to get the name of the component that threw the error.

I'm pretty sure that using the router will work in most cases const throwingComponentName = router.currentRoute.value.name.

However, how do I get the name of the component from the instance?

CodePudding user response:

If your component has its name option defined, you can access it from instance._.type.name:

onErrorCaptured((err, instance, info) => {
  console.log('component name', instance._?.type?.name)
})

demo

Since instance._ is technically an internal/undocumented property, use it with caution, as it could be refactored/removed in a future release. This works as of [email protected].

  • Related