What causes the v-for in a template to only update when the input text changes?

I'm currently working on a Vue.js code, but I'm facing an issue where the template isn't updating when new data is added to the input text. My goal is for the list to update when the @click event occurs.

Visit this link for reference

methods: {
    addModule:function(){
        var size = Object.keys( this.modules ).length;
        this.modules[size] = {
            enumerate: '1.1',
            name: 'test',
            description: 'test d',
            type: 'module',
            criteria: {}
        };
        console.log(this.modules);
    }
}

Answer №1

Seems like you're encountering a challenge with object change detection as outlined in the vue.js documentation. In this case, it appears that when you add a new property to an object that didn't exist previously, the change goes unnoticed.

One way to address this issue is by using the following approach:

methods: {
    addModule:function(){
        var size = Object.keys( this.modules ).length;
        this.modules[size] = {
            enumerate: '1.1',
            name: 'test',
            description: 'test d',
            type: 'module',
            criteria: {}
        };
        this.modules = Object.assign({}, this.modules);
        console.log(this.modules);
    }
}

The addition of this specific line will essentially create a duplicate of the original object. By working with this cloned object instead, any modifications made are guaranteed to be recognized by the system.

Keep in mind, if deep cloning is required, additional steps will be necessary.

Answer №2

Vue.js may not recognize a newly added property in an object. Check out the details at this link

To address this issue, you can utilize Vue.set():

var count = Object.keys( this.items ).length;
        Vue.set(this.items,count,{
            id: '1',
            name: 'example',
            description: 'sample description',
            type: 'item',
            details: {}
        });

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

Streamline your processes by automating jQuery AJAX forms

I am looking to automate a click function using the code snippet below in order to directly submit form votes. Is there a method to develop a standalone script that can submit the votes without requiring a webpage refresh: <input type="submit" id="edit ...

Parsley JS failing to validate

I attempted to validate my form using parsley.js, following the instructions from the official documentation. However, for unknown reasons, it is not functioning as expected. Below is the code snippet: <div class="container"> <div class= ...

recording JSON requests in expressJS

Update: Apologies for the confusion, I have realized that I needed to move the app.use(express.logger('dev')); higher up in the program. This adjustment has resulted in logging every GET and POST request successfully. Express server now listeni ...

Steps for efficiently incorporating a template within another template

Just starting out with VueJS... I'm trying to figure out a way to dynamically add other components into a main component based on the dropdown selection. The main template always remains on the screen and includes a dropdown. What I want is to have a ...

Having trouble integrating select2 with geonames?

I'm currently experiencing difficulties when trying to integrate select2 with geonames. Although I am able to generate a list of cities, I am unable to select any as a valid option. HTML <select id="cities" name= "cities"> <option value=" ...

Trouble arises when adding a .js script to the webpage

I'm feeling quite puzzled by this small piece of code, as it appears to be the simplest thing you'll come across today. Despite that, I can't help but seek guidance because I've been staring at it for what feels like an eternity and can ...

Guide on transferring data from a component to App.vue in Vue 3, even with a router-view in the mix

I have the following layout: src components Footer.vue views Page.vue App.vue I want to access the 'message' vari ...

When attempting to click on my subtopics using jQuery, they do not appear as expected

$(document).ready(function () { $("#subTopics").hide(); $("#mainTopics").click(function () { $("#subTopics").show("slow"); }); }); body { margin: 0; } li, a{ text-decoration: none; list-style-type: none; text-d ...

Encountering the 'unsupported_grant_type' error while attempting to retrieve an access token from the Discord API

Having trouble implementing Discord login on my website. When trying to exchange the code for an access token from https://discord.com/api/oauth2/token, I keep getting the error { error: 'unsupported_grant_type' }. This is my code: const ...

Can someone show me how to code a function that transforms my paragraph into an image when I hover over it?

< p id="p1" onmouseover="this.style.color='transparent';flipPara()"> < p id="p2" onmouseover="spookyPara()"> function spookyPara() { document.getElementById("p1").attribute = "All_Images/ghost.png"; } I currently have this code sn ...

Enhancing x-axis presentation in D3.js-generated charts

I created a bar chart using D3.js, but I have encountered an issue with one of the values on the x-axis being too long. I attempted to use CSS properties like text-overflow: ellipsis, width: 10px, and overflow: hidden to abbreviate values that exceed a cer ...

Automating image uploads with Selenium and Python even when the element appears hidden

Recently, I've encountered an issue while trying to upload photos using Selenium with Python. The input element appears to be hidden on the page, causing errors when using the .sendkeys method. Here is the HTML code for the input element: <div d ...

JQuery is unable to initiate a keyup event

I am currently utilizing jQuery in a web application. On one of my pages, I have set up an event listener for keypresses as shown below: document.addEventListener('keyup', function (event) { event.preventDefault(); var key = event.k ...

Effortlessly Retrieve Initial Data from Ajax in Vue3 using Composition API

After extensively researching different approaches, I am leaning towards using the Vue3 composition API in its "setup" form for future compatibility. However, I am encountering a significant amount of variability across these approaches. My current form i ...

JavaScript error message stating that the setAttribute function is null

As a newcomer to JS, I am currently working on creating a task list webpage that allows users to submit projects and create task lists within each project with designated due dates. In order to generate unique ID tags for projects and tasks, I utilized UU ...

Create a slider feature on a webpage using the Google Maps API

A webpage has been created using the Google Map API. JSfiddle function initMap() { var intervalForAnimation; var count = 0; var map = new google.maps.Map(document.getElementById('map'), { cen ...

The Vue property or method is unrecognized when utilizing the non-minified version

When I attempted to display the name 'John' using an inline template in a simple Vue example, I encountered the following error message: [Vue warn]: Property or method "name" is not defined on the instance but referenced during render. ...

Can you explain the concept of asynchronous in the context of Ajax?

Can you explain the concept of Asynchronous in Ajax? Additionally, how does Ajax determine when to retrieve data without constantly polling the server? ...

Trigger the select dropdown when a key is clicked

I am currently working on a project in react where I am utilizing the Select component from material. When I open the dropdown and press a key on my keyboard, the option starting with that key gets automatically selected. This behavior is also observed in ...

Counting each item with jQuery and assigning them numbers 02, 03, 04, etc., with the exception of the first item which will display as "Up Next

I'm still learning jQuery and here's the code I've put together after researching on stackoverflow and other platforms: var counter = 1; $('.next-page .nav-item').each(function () { if ($(this, ':gt(0)')) { $(this ...