How can I monitor and track modifications to data in Vue.js within an existing JavaScript codebase?

For a project I've been working on, I'm considering incorporating vuejs for some of the UI elements. I followed a tutorial and put together a simple example to get started.

Here is a basic javascript object I have:

var hex = {};
hex.turn_phase = 'unit_placement';

In my template, I set up:

var app = new Vue({
        el: '#vue-test',
        data: {
            message: 'Hello Vue!',
            turn_phase: hex.turn_phase,
        },
        delimiters: ["<%","%>"],
        mounted: function() {
            this.fetch_turn_phase();
        },
        methods: {
            fetch_turn_phase: function() {
                Vue.set(this, 'turn_phase', hex.turn_phase);
            },
        }
    });

Initially, this correctly displays the turn_phase on the template. However, if I update the hex.turn_phase value in the browser console, the template does not update.

Am I overlooking something in this basic example?

Answer №1

Seems like you might be making things more complicated than they need to be. Why not access your Vue instance using the app variable?

Remember to always utilize the setters generated by Vue.js. These setters are being watched and will trigger a re-render of your component.

https://i.sstatic.net/DhgjP.png

Instead of your current method, consider using

app.turn_phase = 'unit_placement';

For a deeper dive into this topic, check out Reactivity

Answer №2

When working with Vue, all data variables, computed properties, and values returned from methods are made reactive. In this scenario, if you modify the variable hex, which is not a Vue variable, Vue will not detect any changes in it. However, if you modify the variable message, it will trigger a reflection in the template.

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

The interval becomes congested due to the execution of two simultaneous Ajax calls

I have an Interval set to run a function every 3 seconds. intervalStepper = window.setInterval('intervalTick()','3000'); function intervalTick() { if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari ...

Start by creating a set of vertices and triangles to be utilized by the vertex shader

After experimenting with vertexshaderart.com, I'm eager to apply what I've learned on a different website. While I have experience using shaders in the past, some effects achieved on the site require access to vertices, lines, and triangles. Pass ...

Unusual actions from the jQuery Steps extension

I am currently utilizing the jQuery Steps plugin (FIND IT HERE). The issue I'm facing lies within an IF statement that is causing the wizard to return to the first step instead of staying on the current indexed step. While all other IF statements are ...

Steps to create a Personal Access token on GitHub

I recently developed a test app that utilizes github's graphQL API. I have included the personal access token directly in the code for authentication purposes. However, every time I commit the code with the token, I receive an email from GitHub with t ...

JavaScript - The function will not execute any code inside its body

When I try to call my constructor function, it stops at the function definition in the debugger and never reaches the actual function body. Is there a common reason for this issue that I might be missing? Here is an example of the code: myconstructor.js ...

Building a static website with the help of Express and making use of a public directory

It seems that I am facing a misunderstanding on how to solve this issue, and despite my efforts in finding an answer, the problem persists. In one of my static sites, the file structure is as follows: --node_modules --index.html --server.js --app.js The ...

"Enhancing web interactivity with AJAX requests and dynamic functionality in web

I'm finding it hard to understand the distinction between Rich Internet Applications and AJAX calls. From what I gather, any application that requires client-side execution can be classified as RIA. So, by this definition, should this website be cons ...

Creating images with LibCanvas

Currently, I am utilizing LibCanvas for canvas drawing on my HTML page. However, I am facing difficulty in drawing an image. Can someone provide assistance with this? UPDATE: The code snippet I am using is showing the image being drawn but then it disappe ...

Exploring the functionality of dimensions in d3 parcoords

I am diving into the world of d3 and experimenting with Behold, a simplistic example directly from the website. <script src="http://d3js.org/d3.v3.min.js"></script> <script src="d3.parcoords.js"></script> <link rel="stylesheet" ...

Encountering a save error in Ubuntu terminal while working on Okta Laravel Vue development

Currently, I am working on a project that involves Laravel, Vue.js, and Okta. I found a helpful tutorial here, but encountered an issue when trying to run the following command: yarn add --save vue-router axios @okta/okta-vue An error popped up stating: ...

Having issues with AngularJS where ng-table/ng-repeat rows are failing to load properly

I've been following a tutorial on using ng-Table, which can be found Here. When I download the example from Git, it loads without any issues. However, when I try to replicate even the simplest example myself, the column headers and filters load but th ...

I am facing issues with jQuery's live and livequery functions when trying to use them with a form that is being loaded dynamically through

Previously, I inquired about a related issue regarding attaching behavior to an element with jQuery after insertion. However, I have not yet found a solution. For clarity's sake, I am posing a new question with a different scenario. Below is the code ...

Tips for updating an ASP.NET MVC page when the browser back button is pressed

Have you ever encountered the issue where clicking the browser back button on any ASP .NET MVC page does nothing, and the page does not update unless you hit F5 to refresh it? It seems that the main problem lies in making changes to the DOM of the page, s ...

``To increase a value within an array object in MongoDB with the use of nodejs, what steps should be

Within my node.js application, I have a MongoDB post model that includes an array of comments. Each comment has a "likes" field representing the number of likes it has received. I want to increment this value for each comment individually. How can I achiev ...

Interactive HTML button capable of triggering dual functionalities

I am working on a project where I need to create a button using HTML that can take user input from a status box and display it on another page. I have successfully implemented the functionality to navigate to another page after clicking the button, but I ...

Populate a form with database information to pre-fill the fields

So I have this web form built with HTML, and there are certain values within the form that can be changed by the user. To ensure these changes are saved, I'm storing them in a database. My goal is to have the form automatically filled with the data fr ...

Directive in AngularJS fails to trigger upon form reset

I encountered an issue with a directive used within a form, specifically when clicking on a button with the type reset. The problem can be replicated on this stackblitz link. The directive I'm using is quite simple - it sets the input in an error stat ...

Troubles with NextJS and TailwindCSS Styling

I encountered a strange issue when I used the component separately. Here's how the code looked like: <> <Head> <title>Staycation | Home</title> <meta name="viewport" content="initial- ...

Modify the text tone within a specific cell

Welcome to my unique webpage where I have implemented a special feature. The text highlighted in red represents the unique identifier for each individual cell. Interestingly, below there is an input field containing the code "0099CC", which corresponds to ...

Strange error message regarding ES6 promises that is difficult to interpret

Snippet getToken(authCode: string): Promise<Token> { return fetch(tokenUrl, { method: "POST" }).then(res => res.json()).then(json => { if (json["error"]) { return Promise.reject(json); } return new Token ...