'How to get current component name in vue js 3.0
I can get component name in vue js 2 by: this.$options.name. How to get it in vue js 3?
Thank you.
Solution 1:[1]
import { useRouter } from "vue-router";
In setup,
const router = useRouter();
const componentName = router.currentRoute.value.name;
Solution 2:[2]
Managed to get it using: this.$parent.componentName.
<script lang="ts">
import {Options, Vue} from 'vue-class-component';
import HelloWorld from '@/components/HelloWorld.vue';
import {Prop} from "vue-property-decorator";
import {Product} from "@/store/models";
@Options({
components: {
HelloWorld,
},
mounted() {
this.$emit('init', this.$parent.componentName + ' mounted')
},
emits: ['init']
})
export default class Home extends Vue {
@Prop(Array) private readonly products!: Product[];
}
</script>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Isamu Arimoto |
Solution 2 | user657009 |