Is it possible to nest v-for directives within a component file?

Even after going through the VueJS tutorials multiple times, I am still unable to find a solution to this problem.

My issue revolves around displaying a list of lists using accordions, which is supposed to work smoothly with vue-strap components.

For example, if I have a list like this:

'myList': [
  ['el 1', 'el 2', 'el 3'], ['el 1', 'el 2'], ['el another']
]

I would expect the following layout:

List 1:

  • el 1
  • el 2
  • el 3

List 2:

  • el 1
  • el 2

List 3:

  • el another

However, for some reason VueJS fails to render this component...

Here's the code snippet:

<template>
  <accordion id="rabo" :one-at-atime="true">
    <template v-for="list in myLists">
      <panel header="List #{{ $index }}" :is-open="true">
        <ul>
          <li v-for="el in list">
            {{ el }}
          </li>
        </ul>
      </panel>
    </template>
  </accordion>
</template>

<style lang="scss" scoped>
</style>

<script>
  import Vue from 'vue'
  import { accordion, panel } from 'vue-strap'

  module.exports = {
    components: {
      'accordion': accordion,
      'panel': panel
    }
  }

  new Vue({
    el: '#rabo',
    data: {
      'myLists': [['el 1', 'el 2', 'el 3'],['el 1','el 2'],['el another']]
    }
  })
</script>

Answer №1

Here are the steps you should follow:

  1. Move the Vue instance to a separate file
  2. Place the myLists array within the data property of the component
  3. Ensure proper binding of the header prop

MyAccordion.vue

<template>
  <accordion :one-at-atime="true">
    <template v-for="list in myLists">
      <panel :header="`List #${$index}`" :is-open="true">
        <ul>
          <li v-for="el in list">
            {{el}}
          </li>
        </ul>
      </panel>
    </template>
  </accordion>
</template>

<script>
  import { accordion, panel } from 'vue-strap'

  export default {
    components: {
      accordion, panel
    },

    data () {
      return {
        myLists: [
          ['el 1', 'el 2', 'el 3'],
          ['el 1', 'el 2'],
          ['el another']
        ]
      }
    }
  }
</script>

Vue instance

import Vue from 'vue'
import MyAccordion from './MyAccordion.vue'

new Vue({
  el: '#demo',
  components: { MyAccordion }
})

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

What could potentially be the reason behind the incapability of the next.js Image component to transform the svg into a

Unique Context I recently developed a minimalist Hero + Navbar using Next.js. The site utilizes the powerful next.js Image component to display images. Surprisingly, all three images on the website, which are in .webp format, load instantly with a size of ...

The image being displayed is significantly wider than the corresponding HTML element

I'm attempting to display this webpage in PNG format using npm's wkhtmltox: <!doctype html> <html> <head> <meta charset="utf-8"> <style> html, body { padding: 0; width: 340px; } ...

Neglecting the inclusion of a property when verifying for empty properties

In the code snippet below, I have implemented a method to check for empty properties and return true if any property is empty. However, I am looking for a way to exclude certain properties from this check. Specifically, I do not want to include generalReal ...

JavaScript code that functions similarly to VLOOKUP, allowing you to map input values from one field to

As a beginner in HTML and JavaScript, I am trying to create a simple form that automatically populates a specific "Customer Code" when a "Customer Name" is selected from a dropdown list (similar to an Excel VLOOKUP). However, the examples I found on Stack ...

The second AJAX call is unsuccessful

I have created a dynamic ajax website that retrieves pages from the /pages folder and displays them within an ajax-container div on my index.php. Now, I am looking to implement a second ajax request that will only be triggered on certain pages. For examp ...

Tips for delaying the execution of numerous ajax success callbacks?

In my JavaScript code, I am facing the following situation: call_A(success_A) call_B(success_B) function call_A(success){ // make ajax request success(result) } function call_B(success){ //make ajax request success(result) } function success_A(){ ...

Displaying the active navigation element in ApyCom JavaScript jQuery menu: tips and tricks

Currently, I am utilizing the jQuery navigation menu from ApyCom. Everything seems to be functioning properly except for one issue. Whenever I click on a different navigation element, I expect that element to remain highlighted in order to indicate to the ...

VueJs has the ability to display/render a single object from a collection of hundreds of objects at a time

When my API returns hundreds of objects in a response, I want to display only one object at a time on a page. The page includes two buttons - Previous and Next - that allow users to navigate between objects. How can I efficiently manage this data retriev ...

.scss compiling with errors

Recently, I embarked on a new Vue(3) project. Within this project, I have set up some basic styling in the App.scss file and created a HomeView.vue with a corresponding HomeView.scss file (located in the /src/views/Home directory). The styling from both fi ...

Filtering JSON data by date range in React JS

I have a dataset with JSON data containing ISO dates, and my goal is to filter out entries based on the "date_created" field falling within a specific date range. The time component should be ignored in this comparison, and the original JSON data should re ...

The history mode router in Vue disrupts the dynamic URL hashing

When using Vue version 2.6.14 and setting Vue Router to history mode, encountering a URL with a hashtag "#" can cause issues with dynamic paths. const router = new VueRouter({ base: `${process.env.VUE_APP_PUBLIC_PATH}`, mode: 'history', rou ...

Calculating an array of relationships within Laravel by utilizing Vuejs

I am attempting to utilize Vuejs to sum the amount array in Laravel Relationships using computed properties. https://i.stack.imgur.com/MiMxP.png However, it is currently returning a NaN result... computed: { subTotal() { return th ...

Why does my VueJS method only execute after I save changes in VS Code instead of reloading the page? Can someone provide assistance?

Whenever I refresh the page, the console.log inside the method does not get executed until I make a change in VSC and save it. Is the show/hide modal for the form causing this issue? I tested removing the toggle but the problem persisted. Please refer to ...

Do not use npm to install underscore libraries

How can I resolve the error I encountered while attempting to install packages using npm? Here is my packages file: "dependencies": { "express": "~3.3.6", "socket.io": "0.9.16", "jade": "~0.35.0", "less-middleware": "~0.1.12", "redis ...

After deploying on Vercel, Next.js' getServerSideProps function is returning undefined

I am trying to create a Netflix-inspired website using next.js. I am able to fetch movie data from TMDB using getServerSideProps(). While everything works as expected in development mode, once deployed on Vercel (re-deployed multiple times), the props I re ...

The issue of an unsuccessful Ajax call arises in a WordPress plugin when utilizing wp_remote_get

Encountering difficulties with the wp_remote_get function in my Wordpress plugin. The objective is to invoke a method within my primary public class using ajax. However, every time I attempt to make the call with the wp_remote_get function, it fails. This ...

jQuery does not support animations for sliding up or down

I'm struggling to conceal a navigation element once the top of the #preFooter section is scrolled to. In order to make the nav element mobile-friendly, I have designed both the .tab-wrap and .tab-wrap-mobile. To enable these elements to slide away w ...

What is the method for eliminating PHP $_SESSION using AJAX?

I am facing an issue with removing an array within a PHP Session variable using AJAX. Here is the process I follow: HTML: <a href="#" onclick="delete_pix(false, '1', false, '1.jpg');">remove</a> JavaScript: functio ...

Adjust the loading bar component in Material UI to allow for customizable color changes

I am currently learning how to use material ui. My goal is to customize the loading bar's CSS. I checked the documentation and utilized colorPrimary classes. However, the changes are not appearing as expected. Could you please guide me on how to resol ...

`How can I effectively integrate react-i18next with the Semantic UI label element?`

Currently, I am working with Semantic UI along with the integration of [react-i18next][2]. My goal is to enable translation for label strings, but these labels include HTML tags, such as span. Unfortunately, the system only allows hardcoded or variable s ...