DEV Community

Cover image for How to get the "name" of a Component when looping through Components
Mark Abeto
Mark Abeto

Posted on β€’ Edited on

4 1

How to get the "name" of a Component when looping through Components

Hi Good afternoon Guys πŸ˜„.

Basically, I had a problem today getting the name of a component so I can use that name as a dynamic prop title in a span tag. So when I hover on that span I can see the name of that component.

A Sample SVG component.
SVG Comp

The Project.vue file.
Project.vue

The CardProject.vue file.
CardProject.vue

I wanna have a span tag above the component tag with a title property.

Maybe we can change our projectTechs array to look like this.
Yuck

Using this would change our code in the v-for directive code and it adds a lot of code and it makes our code ugly. I didn't use this approach. So I tried googling the solution for this and I found to get the name of the component you have to use this this.$options.name. I tried using it.

dumb move

And it threw an error.

  vue.runtime.esm.js?2b0e:1888 TypeError: Cannot read property 'name' of undefined
Enter fullscreen mode Exit fullscreen mode

What does this mean is that there is no $options property in the tech object.

So I tried logging the technologies array in the console.
And I found out there is a name property in each of the components in the array that you can access directly.

Now my CardProject.vue file looks like this.
Updated CardProject

Thanks for reading this post.

Have a Nice Day Everyone πŸ˜ƒ.

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free β†’