0

I am aiming to create a navigation menu, where each div will have a header and a list of links

<div>
  <h1>Header</h1>
    <ul>
      <li>
        <a href="#">Link</a>
      </li>
    </ul>
</div>

I want to add the link data to the data() in Vue, and I have attempted it like so:

<script>
    export default {
        data() {
            return {
                links: {
                    columns: [
                        {
                            heading: 'Feature',
                            attr: [
                                {
                                    text: 'Something'
                                }
                            ]
                        }
                    ]
                }
            }
        }
    }
</script>

And here is the HTML with the attempt to use v-for

<template>
    <div class="pt-8 sm:pt-16 h-screen navigation-background-gradient">
        <nav class="mx-4 sm:mx-48">
            <div class="sm:grid sm:grid-cols-3 sm:gap-56">
                <div v-for="link in links.columns">
                    <h1 class="mb-5 text-white font-semibold font-montserrat tracking-wider uppercase text-2xl text-shadow-glow">{{ link.heading }}</h1>
                    <ul class="space-y-4">
                        <li>
                            <a class="block text-lg text-menu-link font-inter font-medium tracking-wider" href="#">{{ link.attr }}</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
    </div>
</template>

So I can access the heading fine, but I can't access attr.text

If I echo out attr.text I get [ { "text": "Something" } ] but I aren't sure how to get to it. I thought I may need to do another loop, as it's an array, but I couldn't work out the correct combination to use to make it show.

I feel like I am close and missing something obvious, but I can't see it. Can anyone suggest me where am I doing wrong ?

Dcoder14
  • 1,831
  • 3
  • 12
  • 22
Chris Mellor
  • 347
  • 5
  • 15

2 Answers2

0

@Chris, attr is an array so you have to access this by index. So for your case, you can do like

{{ link.attr[0].text }}

As your code shows attr array contains only one item so I have done attr[0] as to get first item of an array we use 0 index.
And If your attr array contains more than one element then you have to loop through another loop as used in below link
Nested loop in Vue

Dcoder14
  • 1,831
  • 3
  • 12
  • 22
0

Thanks to @Dcoder, who pointed me in the right direction, I was able to work it out. I will post here in case others come across this - hopefully it might be helpful!

I had to do another loop on the <li> but using the array from the first loop (links.columns), so my second loop consists of:

<li v-for="attribute in link.attr">
  <a href="#">{{ attribute.text }}</a>
</li>

As you can see, link stores the rest of the array data, and as attr is also an array, I can loop through that.

Chris Mellor
  • 347
  • 5
  • 15