Exploring Deeper: VueJS Third Level NavigationدهNavigating the Depths

Hello, I am still learning VueJS and need some help with my nested navigation. The categories go 3 levels deep, but I'm having trouble displaying the third level properly.

Here is the JSON data I'm working with:


return {
  navList: [
    { id: 1, type: 'Item', url: "#", name: "About Us", target: '_blank' },
    { id: 2, type: 'Item',url: "#", name: "Story", target: '_blank' },
    { id: 3, type: 'Item',url: "#", name: "Price", target: '_blank' },
    {
      id: 4, 
      type: 'Item',
      url: "#",
      name: "Services",
      target: '_blank',
      children: [
        {
          url: "https://twitter.com/",
          name: "Twitter",
          target: "_blank",
          child: [
            {
              url: "https://twitter.com/",
              name: "Twitter",
              target: "_blank",
            },
            {
              url: "https://dribbble.com/",
              name: "Dribbble",
              target: "_blank"
            },
            {
              url: "https://www.behance.net/",
              name: "Behance",
              target: "_blank"
            },
          ],
        },
        {
          url: "https://dribbble.com/",
          name: "Dribbble",
          target: "_blank"
        },
      ]
    },
  ]
};

Below is the code for my navigation that I'm using to try and display the sub-categories:

I'm attempting to access them through `item.children.child`, but it's not showing up as expected, instead, it just displays "Service". Please refer to the screenshot below for more clarity:


<template>
  <nav class="navbar navbar-light navbar-expand-lg mainmenu">
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbar">
      <ul class="navbar-nav mr-auto">
        <li v-for="item in navList" :key="item.id" class="dropdown">
          <template v-if="item.children">
            <a class="dropdown-toggle" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"
              :id="item.name"
              :href="item.url" 
              :title="item.name" 
              @click="show = !show">{{ item.name }}
            </a>
            <ul class="dropdown-menu" 
                  :class="{ show }"  
                  :aria-labelledby="item.name">
                <li class="dropdown-item" v-for="{ url, name, index, target } in item.children" :key="index" >
                    <a class="dropdown-toggle" role="button" aria-haspopup="true" aria-expanded="false"
                      :href="url" 
                      :title="name" 
                      :target="target"
                      :data-toggle="name">
                      {{ name }}
                    </a> 
                    <ul class="dropdown-menu" 
                      :aria-labelledby="name">
                      <template v-if="item.children.child">
                        <li class="dropdown-item" v-for="{ url, name, index, target } in item.children.child" :key="index" >
                        <a class="dropdown-toggle" role="button" aria-haspopup="true" aria-expanded="false"
                          :href="url" 
                          :title="name" 
                          :target="target"
                          :data-toggle="name">
                          {{ name }}
                        </a> 
                        </li>
                      </template>
                      <template v-else>
                        <a class="nav-link"
                          :href="item.url" 
                          :title="item.name"
                          :target="item.target"
                          >{{ item.name }}</a>
                      </template>
                    </ul> 
                </li>
            </ul>
          </template>
          <template v-else>
              <a class="nav-link"
                :href="item.url" 
                :title="item.name"
                :target="item.target"
                >{{ item.name }}</a>
          </template>
        </li>
      </ul>
    </div>
  </nav>
</template>

If you want a visual of what I'm experiencing, check out this screenshot.

Answer №1

The issue lies in the

v-for="{ url, name, index, target } in item.children"
part of the code. When using v-for, you should either use v-for="item in items" or
v-for="(item, index) in items"
syntax for arrays. In this case, you are using the first option which attempts to destructure { url, name, index, target } properties from your items. This results in the index being undefined since your items do not contain such a property, which leads to the list items lacking unique :key property. Consider using the following syntax instead:

v-for="({ url, name, target }, index) in item.children"

For further reference: https://v2.vuejs.org/v2/guide/list.html

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue arises when isomorphic-dompurify is used alongside dompurify in Next.js 13 causing compatibility problems

I am currently facing a compatibility problem involving isomorphic-dompurify and dompurify in my Next.js 13 project. It appears that both libraries are incompatible due to their dependencies on canvas, and I am struggling to find a suitable alternative. M ...

What is the best way to dynamically change the JSON-LD Script for the Schema?

Below is the script in question. Please read through it carefully. <script type="application/ld+json"> { "@context": "http://schema.org/", "@type": "Product", "name": "Bat, &q ...

Learn how to gradually make text disappear and reappear using JavaScript or jQuery

I am a beginner in JavaScript and jQuery, and I am trying to achieve a text fade-out effect. Once the text has completely faded out, I want it to return with the same fade-in effect. I have been exploring if statements and fadeIn effects, but I am strugg ...

Leveraging Next.js for "client-side rendering" in React Encapsulations

My dilemma involves a component housing content crucial for SEO optimization. Here is an excerpt from my code: <AnimatedElement className="flex flex-col justify-start items-center gap-4 w-full" duration={500}> <h2 class ...

Is it necessary to include index.js in each component in a react project?

After checking out an article on the benefits of using a Folder Components structure, I came across this link. The author praised the use of index.js as a solution, but there was also a comment from someone arguing that it's unnecessary to have index. ...

Can regular JavaScript objects contain jQuery objects?

Lately, I've been working on optimizing the code for a carousel I built. One idea I'm toying with is organizing all my variables and functions into a JavaScript object to keep them separate from the rest of the code in the file (which happens to ...

Tips on transmitting multiple byteIO documents from Flask endpoint to Vue via axios and file encoding

Summary Flask: docx --> bytesIO --encodebytes()--> bytes --decode()--> string --> array containing multiple of those strings. Vue: array containing those strings --?--> blob Inquiry I am attempting to send multiple byteIO files from a ...

Vuex catches us by surprise by simultaneously updating two state objects

My task involves handling a list of companies. The user can select a company by clicking on a list item, which sets it as "selectedCompany" in the store and displays the single item view. To enable editing of different sections, the user can switch to edit ...

Can a value of a variable be "stored" in NodeJS?

I am working on a website that allows clients to make their site go live by setting var live = true;. Once this variable is set, certain webpages will display. I would prefer not to store the live variable in a database as creating a collection solely fo ...

Deploy several Vuejs components within a single project on npm using webpack

I am attempting to publish a project on npm that includes multiple Vue components. My goal is to import, register, and utilize both components in the following manner: import Component1 from 'npm-package' import Component2 from 'npm-package ...

Vue Bootstrap Checkbox and <b-table> with <b-form-checkbox> components combine to provide a powerful and flexible user

I am currently implementing b-form-checkbox with b-table to fetch the selected module Ids. <b-table id="module-table" :items="list.modules" :fields="fields" :busy="isBusy"> <template slo ...

What is the process for implementing a component in antdesign when using vue-cli and vue 3?

I followed the instructions provided in the documentation here. These are the steps I took: vue create example-app cd example-app I selected the vue 3 preset. Then, I made changes to the script in main.js import Vue from 'vue'; import Button f ...

Retrieving HTML content using scriptJS

Currently utilizing VueJS and encountering an issue after compiling code in production mode. I have one index.html file and several assets that are being uploaded to the cloud. The challenge lies in the fact that the client is not accepting the HTML file ...

JavaScript - Asynchronous JavaScript and XML

function sendRequest(settings) { settings = { type: settings.type || "POST", url: settings.url || "", timeout: settings.timeout || 5000, onComplete: settings.onComplete || function(){}, onError: settings.onError || function(){}, onSuccess: set ...

Transferring a string between Python and Javascript

I am encountering significant challenges while attempting to pass a string from Python to Javascript via an ajax POST request. I have experimented with using JSON and without, but both approaches have not been successful. Below is the code snippet: JAVA ...

JavaScript HTTP request causes website navbar animation to malfunction

Currently, I am assisting a client with their static website. This task isn't my primary role, but due to some expertise in this area, I have volunteered to support them. To streamline the process and avoid duplicating HTML code across multiple files, ...

Step-by-step guide on integrating a specific location into Google Maps using React.js

I'm in the process of revamping my website using Reactjs. I want to incorporate a specific Google location with reviews on the map, similar to how it appears on this example (My current website is built on Wordpress). As of now, all I've been ab ...

Vue.js V-for not rendering any content

I am currently working on populating an array with objects retrieved from an API. The objects are being fetched successfully, but I am facing issues when trying to display them using v-for in the array. Below are my data variables: data() { return { ...

Ensure selected language is maintained when refreshing or changing view by utilizing switch i18n functionality

Hello there, I am facing a challenge with "JavaScript Localization" on my website. The issue is that I cannot figure out how to prevent the DOM from prioritizing the local language of the browser and instead use the language set in the switch as a referenc ...

JSON parsing encountered an unexpected token "J" at the beginning of the file, causing an error at line 4 of the mail.js API

I am struggling to identify the issue at hand and determine the root cause of the problem In my efforts to create a contact form, I have encountered an unexpected outcome. When I use console.log() on the FormData object, it shows an empty array and trigge ...