Using the v-for directive in Vue.js 2 with a transition-group

I am working with a projects object that I populate after making an axios call. Then, I am using a v-for directive to loop through the projects object. Here is the code snippet:

<ul class="row projects-list"> 
    <li v-for="project in projects" :key="project.id">
        @{{ project.project_name }} <br>
        <transition-group tag="ul" enter-active-class="animated fadeInUp" leave-active-class="animated fadeInDown">
            <li v-for="board in project.boards" :key="board.id">@{{ board.board_name }}</li>
        </transition-group>  
    </li>        
</ul>

Within the projects object, there is also an object named boards, as shown in the above code. My objective is to animate the rendering of the boards object. However, I encounter the following errors:

[Vue warn]: Property or method "project" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.

and

[Vue warn]: Error in render: "TypeError: Cannot read property 'boards' of undefined"

How can I properly render my transition-group? Is there a workaround for this issue?

Here's a Vue instance:

const app = new Vue({
    el: '#app',
    data: {
        user: {!! Auth::user() !!},
        projects: {}
    },
    methods: {
        fetchProjects() {
            axios.get(`/api/projects/${this.user.id}`)
                .then((response) => {
                    if(response.status === 200) {
                        this.projects = response.data;
                        console.log(this.projects);
                    }
                })
                .catch(function(error) {
                    console.error(error);
                });
        }
    },
    mounted() {
        this.fetchProjects();
    }
});

Answer №1

As I iterate through the projects object using the v-for directive.

If your "projects" is an object, then in this case "project" refers to the attributes of the projects object that you are looping through. Based on your code, it seems like projects and boards should actually be arrays, not objects.

You need to first verify if the response data for projects is actually an array.

Scenario I:

var objects = [{id:1, project_name:'aaa'},{id:2, project_name:'bbb'}]

result: aaa bbb

Scenario II:

var objects = {attr1: {id:1, project_name:'aaa'}, attr2: {id:2, project_name:'bbb'}}

result: aaa bbb (although the order is not guaranteed.)

Answer №2

Vue.js has replaced all elements inside the component, rendering your code for <li> useless. In order to make it functional, you must pass the project as a prop to the transition-group component.

Vue.component('transition-group',{
props:['project'],
template:`<div>
    <li v-for="board in project.boards" :key="board.id">{{ board.board_name }}</li>
    </div>
  `
});

const app = new Vue({
    el: '#app',
    data: {
        user: {name:'niklesh raut'},
        projects: []
    },
    methods: {
        fetchProjects() {
            this.projects = [{id:1,project_name:'test1',boards:[{id:11,board_name:'board_name11'}]},{id:2,project_name:'test2',boards:[{id:11,board_name:'board_name22'}]}]
        }
    },
    mounted() {
        this.fetchProjects();
    }
});
<script src="https://npmcdn.com/vue/dist/vue.js"></script>

<div id="app" class="container-fluid">
    <ul class="row projects-list"> 
      <li v-for="project in projects" :key="project.id">
          {{ project.project_name }} <br>
          <transition-group :project="project" tag="ul" enter-active-class="animated fadeInUp" leave-active-class="animated fadeInDown">
        </transition-group>
      </li>        
  </ul>
</div>

JSFiddle for the same

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

Extracting CSS data and storing it in a SQL database

Hello there, I've created a div using CSS like this: echo '#'. $pess2['naam'] .' { width: 190px; height: 90px; padding: 0.5em; float: left; left:'. $pess2['left'] .'px; top:'. $pess2['top'] ...

Error: The value for 'prepareStyles' property is undefined and cannot be read

Can anyone provide some guidance on this code snippet? I am struggling to understand the error message I am receiving. import React, {PropTypes} from 'react'; import TransactionListRow from './TransactionListRow'; import {Table, TableB ...

The Lightslider’s responsive height is being clipped

I attempted to utilize the Lightslider plugin in order to craft a responsive slider for showcasing my portfolio images. However, I encountered an issue where the height of each thumbnail in the portfolio appears to be cropped from both the top and bottom ...

I'm trying to determine in Stencil JS if a button has been clicked in a separate component within a different class. Can anyone assist

I've created a component named button.tsx, which contains a function that performs specific tasks when the button is clicked. The function this.saveSearch triggers the saveSearch() function. button.tsx {((this.test1) || this.selectedExistingId) && ...

Attempting to integrate Bootstrap calendar onto my website, unfortunately receiving an Uncaught Error

I keep encountering this specific error: "Uncaught Error: Calendar: Events load URL is not set". Even though I am meticulously following the guidelines at https://github.com/Serhioromano/bootstrap-calendar, the calendar fails to display. All I ...

When running the command "npx create-next-app@latest --ts," be aware that there are 3 high severity vulnerabilities present. One of the vulnerabilities is that "node-fetch

Just set up a fresh project with Next.js and TypeScript by following the documentation using npx create-next-app@latest --ts. Encountering multiple high severity vulnerabilities despite running npm audit fix --force, which actually adds more vulnerabiliti ...

Ways to integrate npm dependencies into your Cordova plugin

Currently working on implementing a Cordova plugin called core-cordova found in this repository. This particular plugin has a dependency on another NPM package. The issue arises after installing the plugin in my app using: $ cordova plugin add @aerogears ...

How to Utilize Muuri Javascript Library with Bootstrap Tabs: Resizing Required for Expansion?

Struggling for days and feeling frustrated, I'm hoping someone can help me crack this mystery. My idea is straightforward - three Bootstrap 4 tabs with drag and drop functionality inside each, all displayed in a responsive stacked masonry layout. To a ...

Expanding the MatBottomSheet's Width: A Guide

The CSS provided above is specifically for increasing the height of an element, but not its width: .mat-bottom-sheet-container { min-height: 100vh; min-width: 100vw; margin-top: 80px; } Does anyone have a solution for expanding the width of MatBott ...

Generate selectable options dynamically in real-time

In my code, I am working with two select elements - one filled with data and the other one empty. The idea is that when a selection is made in the first select element, I use a switch statement to capture that event. Based on the value selected, I then aim ...

transform array elements into an object

I encountered the following code snippet: const calcRowCssClasses = (<string[]>context.dataItem.cssClasses).map( (cssClass) => { return { [cssClass]: true }; } ); This code block generates an array of objects like ...

Unleashing the full potential of ajax through endless scrolling performance

Recently, I implemented an infinite scroll feature on my website's index page by making ajax requests to retrieve a JSON containing 40 objects. Then, I add them using a JavaScript loop. However, I've noticed that it slows down the site at times. ...

What could be causing Axios to send requests prior to the component mounting?

My application consists of a React frontend and a Spring backend. I am using Axios to fetch data from the backend. There are two class components containing tables, which can be accessed through a menu component (only in componentDidMount and componentDidU ...

Assigning multiple properties to objects in Javascript

Can you show me a quicker way to do this? object.position.x = position.x object.position.y = position.y object.position.z = position.z object.rotation.x = rotation.x object.rotation.y = rotation.y object.rotation.z = rotation.z Your assistance is greatly ...

Attempting to scroll through a webpage and extract data using Python and Selenium in a continuous manner

Recently, I posed a question (you can find it here: Python Web Scraping (Beautiful Soup, Selenium and PhantomJS): Only scraping part of full page) that shed light on an issue I encountered while attempting to scrape all the data from a webpage that updates ...

Cover the entire screen with numerous DIV elements

Situation: I am currently tackling a web design project that involves filling the entire screen with 60px x 60px DIVs. These DIVs act as tiles on a virtual wall, each changing color randomly when hovered over. Issue: The challenge arises when the monitor ...

Encountering an issue with sending a direct message on Twitter through OAuth

I am currently developing a JavaScript script to work alongside my C++ application for sending direct messages to users. The script is responsible for constructing the request that I send out. However, whenever I make a request, I keep getting errors like ...

While the Navbar component functions properly under regular circumstances, it experiences difficulties when used in conjunction with getStaticProps

https://i.stack.imgur.com/zmnYu.pngI have been facing an issue while trying to implement getstaticprops on my page. Whenever I try to include my navbar component, the console throws an error stating that the element type is invalid. Interestingly, I am abl ...

I am looking to organize my content by creating categories such as How, When, and Why for different videos. How can I achieve this for a large amount of text

I am in the process of organizing categories for the text I have. My goal is to display text from specific categories based on the color that a random video lands on. For example, if the video lands on the color red, I want text from the category labeled " ...

Guide on redirecting to a specific Vue-app page using Flask

I am currently working on an application that includes a page that ends with '@' and provides meta information for the page without '@'. For example, if the page '/user/aabb' contains information about the user 'aabb&apos ...