Solsiden
Solsiden

Reputation: 784

How to get the component's key in Vue 3 composition setup() api?

In the regular API I can get the key like this:

created()
{
    console.log(this._.vnode.key);
}

But in the composition API, in setup(), 'this' is undefined. I tried to check the data and context properties, but can't find anything.

Upvotes: 3

Views: 1494

Answers (1)

Solsiden
Solsiden

Reputation: 784

Figured it out:

import { getCurrentInstance } from "vue";

setup() {
    const component = getCurrentInstance();
    console.log(component.vnode.key);
}

Upvotes: 4

Related Questions