Infinite loop and problem with v-show in Vue.js

1

I'm doing a project in and I have two situations. The first is that I am generating a list (which is repeated twice) using a v-for , which works correctly, but in the console I get the following error:

  

[Vue warn]: You may have an infinite update loop in a component render   function.

I understand that the error has to do with something I'm doing wrong in the loop.

The other problem is that I want to show a menu with v-show , but for some reason it's not working, and I think all things are in their place. Maybe it has to do with the first problem.

HTML

<template>
  <header>
    <div>

      <div class="right-content">

        <ul>
          <li v-for="topMenuLink in topMenuLinks" :key="topMenuLink">
            <a :href="topMenuLink.url">{{topMenuLink.text}}</a>
          </li>
        </ul>

        <div class="menu">
          <a href="javascript:void(0);" class="menu-button" :click="showMenu = !showMenu">Menu</a>
        </div>
      </div>
    </div>
    <ul class="dropdown-menu" v-show="showMenu">
      <li>item 1</li>
      <li>item 2</li>
      <li>item 3</li>
      <li v-for="topMenuLink in topMenuLinks" :key="topMenuLink"><a :href="topMenuLink.url">{{topMenuLink.text}}</a></li>
    </ul>
  </header>
</template>

Vue.js

  export default {
    data() {
      return {
        showMenu: false,
        topMenuLinks: [
          {
            text: 'Texto 1',
            url: ''
          },
          {
            text: 'Texto 2',
            url: ''
          },
          {
            text: 'Texto 3',
            url: ''
          },
          {
            text: 'Texto 4',
            url: ''
          }
        ]
      }
    }
  }
    
asked by TheWoodStudio 30.07.2018 в 17:37
source

1 answer

1

First of all, I recommend you have an identifier (id) in :key of the cycle. Modify the data() property so that it stays like this:

data() {
  return {
    showMenu: false,
    topMenuLinks: [
      {
        id: 1,
        text: 'Texto 1',
        url: ''
      },
      {
        id: 2,
        text: 'Texto 2',
        url: ''
      },
      {
        id: 3,
        text: 'Texto 3',
        url: ''
      },
      {
        id: 4,
        text: 'Texto 4',
        url: ''
      }
    ]
  }
}

The biggest change was in the click event, from :click to v-on:click The new HTML would be this:

<header>
<div>
  <div class="right-content">

    <ul>
      <li v-for="topMenuLink in topMenuLinks" :key="topMenuLink.id">
        <a :href="topMenuLink.url">{{topMenuLink.text}}</a>
      </li>
    </ul>

    <div class="menu">
      <a href="javascript:void(0);" class="menu-button" v-on:click="showMenu=!showMenu">Menu</a>
    </div>
  </div>
</div>
<ul class="dropdown-menu" v-show="showMenu" >
  <li>item 1</li>
  <li>item 2</li>
  <li>item 3</li>
  <li v-for="topMenuLink in topMenuLinks" :key="topMenuLink.id">
    <a :href="topMenuLink.url">{{topMenuLink.text}}</a>
  </li>
</ul>

I leave you an example of the code working. link

    
answered by 30.07.2018 / 19:54
source