When the objects in the v-for loop are modified, the rerender does not occur to update the changes in

My issue involves using mounted(), along with reloadComparisons(), and this specific tag:

<li v-for="comparison in comparisons">C: [[ comparison.area ]]</li>

The problem arises when the rendering only occurs if comparisons is defined within data. When I load a new array, it fails to work.

I have attempted to use

Vue.set(this.comparisons,comparisons)
, but unfortunately, it does not trigger any reaction.

Any suggestions on what steps could be taken?

EDIT

var app = new Vue({
            delimiters: ['[[', ']]'],
            el: '#vue',
            data: {
                comparisons: [{'area': 'xxxx'}],
            },
            mounted() {
                this.reloadComparisons()
            },
            methods: {
                reloadComparisons: function () {
                    console.log('reloadComparisons');
                    axios.get("http://127.0.0.1:8000/alex/api/pricemap_comparisons/").then(function (response) {
                        console.log(response);
                        if (response.status === 200) {
                            this.comparisons = response.data.results;
                            Vue.set(this.comparisons, response.data.results);
                            console.log(this.comparisons);
                        }
                    }).catch()
                }
            }
        });

Answer №1

It appears that there may be a misunderstanding with the this context in your code. To rectify this issue, consider implementing the following:

reloadComparisons: function () {
    console.log('reloadComparisons');
    const that = this;                   
  axios.get("http://127.0.0.1:8000/alex/api/pricemap_comparisons/")
    .then(function (response) {
        console.log(response);
        if (response.status === 200) {
           that.comparisons = response.data.results;
           Vue.set(that.comparisons, response.data.results);
           console.log(that.comparisons);
        }
     )}.catch()
   }

Alternatively, for an improved solution: If you are utilizing webpack or other modern build chains with babel, consider using arrow functions to maintain the correct context.

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

Utilize JavaScript to parse JSON response data, calculate the average of a specified field while keeping track of

The data returned from the API call is structured as follows: var array = {"scores":[ { userid: "1", mark: 4.3 }, { userid: "1", mark: 3.8 }, { userid: "2", mark: 4.6 }, { userid: "2&quo ...

Having trouble retrieving the desired information within the JSON format. What exactly is the discrepancy between the two sets of data?

I've researched extensively on similar issues but have not been able to find a solution. My goal is to retrieve the name and value of a coin. The Coinmarketcap API has 2 endpoints. The first endpoint, as indicated in the comment, provides the desired ...

Change this npm script into a gulp task

I have a script in npm that I would like to convert into a gulp task. "scripts": { "lint": "eslint .", "start": "npm run build:sdk && node .", "posttest": "npm run lint && nsp check", "build:sdk": "./node_modules/.bin/lb- ...

Working with jQuery.ajax Function Calls in Array Filter Operations with Javascript

My knowledge of AJAX calls is limited, so I am unsure about how they interact with array filtering using A.filter(). The array in question is used to populate a template synchronously. // An event triggers a function to filter a list on the page. // The f ...

An effective method for regularly uploading JSON data to the backend

As a newcomer to JS development, I am seeking advice on implementing a solution for efficiently loading a large amount of data into backend memory periodically. The goal is to have this data readily available for the UI without causing delays. Here's ...

Ensure Safari sends the Origin header in jQuery GET requests

When I send a request from https://app.example.com, the following code is executed: $.get('https://api.example.com', { foo: 'bar' }) .success(getSuccess) .error(getError); This script runs smoothly on Chrome and Firefox, however, ...

How can you include a comma between two objects within a string, excluding the last object, using JavaScript?

I have a string object stored in a variable passed from another class. My question is how can I add a comma between two objects, excluding the last object? Below is my code snippet: { "id":"57e4d12e53a5a", "body":"asdas", "publishe ...

Explain the concept of DOM components in relation to refs within the React JS framework

Before this issue came up in the React documentation for ref forwarding, there was a mention: The concept of ref forwarding doesn't just apply to DOM elements. It can also be used with class components. There's a discussion on another platform ...

Tips for properly documenting varying numbers of parameters in specific scenarios using JSDoc

Is there a way to document in JSDoc that my function only requires the second and third parameters when the first one is not equal to 3? getTimeframe: function(timeframe, since, until) { /* * @param {Number} timeframe Can be 0, 1, 2, or 3 * @param {Nu ...

Revise a button using JavaScript

Hey there, I could really use some assistance. Currently, I am learning Javascript as a part of a project I am working on and I seem to have run into a bit of an issue with updating the display status of a button on my webpage. I am trying to set up a ...

Struggling with navigating segmented dropdown buttons

I was following a tutorial on Udemy to learn about inputs and dropdown buttons for bootstrap. Everything was going well until I attempted to create a simple selection of albums for one of my favorite artists. The buttons kept separating when I tried to cen ...

Class for Eliminating the Background Image Using Bootstrap

Is there a Bootstrap class that can be used to remove a background image from a div? Currently, I have this style defined in my CSS: background-image: linear-gradient(to bottom, rgba(0,0,0,0.1), rgba(0,0,0,0)); I would like to remove it using: bg-img-non ...

Flask application experiencing JavaScript issues on local host, yet functioning properly when accessed via 127.0.0.1

My web application is built using Python with Flask for the server, and HTML with JavaScript for the user interface that utilizes callback functions and POST methods. While the application runs smoothly with redirections and REST API calls at: Upon click ...

Read and manipulate website content using PHP

I recently encountered a challenging situation as a newcomer. Despite searching on Google, I couldn't find any information about it. There is a website that allows users to search for doctors in their area or state. It's possible that the number ...

What is the process for restarting the timer within a timer controlled by the react-countdown component?

Looking for guidance on how to implement a reset() function in ReactJS and JavaScript to reset a timer when the "Reset Clock" button is clicked on my webpage. The goal is to have the timer return to its initial value. Open to suggestions on how to achieve ...

My messages are falling on deaf ears as the Discord bot remains unresponsive (js)

After creating a Discord bot using node.js and VS Code, I am encountering an issue where the bot appears online but does not respond to messages. Despite ensuring that the bot has all the necessary permissions, I cannot identify the root cause of this prob ...

Tips for forwarding properties in BootstrapVue to a router-link

Looking at how BootstrapVue's navbar is implemented in the documentation here, I realize that for a specific view, I need to pass a prop to it from the navbar. Adding a prop in raw Vue is straightforward, but it seems like BootstrapVue does not provi ...

When the Ionic app is relaunched from the side menu, the view fails to refresh

When I open my Side Menu, I initially see two options - scan barcode or search product. Once I choose one, the rest of the view is filled in dynamically. The issue arises when I try to go back to the Side Menu and reload the view to only display the origin ...

Delay the next loop iteration until receiving an HTTP request

I have a task where I am collecting YouTube video IDs and storing them in an array. Then, I am looping through each video ID to determine the size of that ID's thumbnail image. My challenge now is making sure my loop waits for the HTTP request to fini ...

Modifying an object property by replacing it with a different value stored in the same object - JavaScript

Consider the object below: { id: 1, name: 'jdoe', currentDayHours: null, totalHours: [{ task: 'cleaning', hours: 10}, { task: 'reading', hours: 2 }]} A function is needed to update the currentDayHours based on the task param ...