What could be causing the Vue data to fail to update in this particular scenario?

I'm having trouble grasping Vue completely. For example, consider the following code snippets:

<!--store.js-->
export default {
  x:{
    p:{},
  },
}
<!--text.vue-->
<template>
  <div class="test-comp">Test component</div>
</template>

<script>
import store from '../store';
export default {
   data(){
     return {
       p:store.x.p,
     };
   },
   methods:{
      loadData(){
        store.x.p.a='some data';
      },
   },
   mounted(){
     this.loadData();
   }
}
</script>

After running the file, in Vue, this.p ends up being an empty object {}. However, in plain JavaScript, when I do let p=store.x.p, it creates a reference so that p points to the same value as store.x.p.

What am I missing or doing incorrectly in my Vue implementation?

My apologies, everyone. It seems like there might be a mistake elsewhere because this code works fine on fiddle.

Answer №1

Vue State Management serves multiple purposes, one of which is ensuring that state variables remain reactive. For a detailed explanation, I recommend checking out this resource on the official documentation

If you're interested in creating your own vanilla store, you can update it using either Vue.set or this.$set.

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

Which is the optimal choice: subscribing from within a subscription or incorporating rxjs concat with tap?

After storing data in the backend, I proceed to retrieve all reserved data for that specific item. It is crucial that the data retrieval happens only after the reservation process to ensure its inclusion. Presented with two possible solutions, I am cont ...

Is a missing dependency causing an issue with the React Hook useEffect?

I've encountered an issue with the following code snippet, which seems to only depend on [page]. Despite this, I am receiving the error message: React Hook useEffect has a missing dependency I've come across similar discussions suggesting to com ...

Display only certain links using CSS or JavaScript/jQuery within a widget that appears once the webpage is fully loaded

I'm currently facing an issue on a website that requires a solution involving jQuery/javascript, which I am not proficient in. After the page loads, javascript is executed which renders various html/css elements. The specific portion we are focusing ...

How do I extract a parameter when passing it to a promise in NodeJS?

Let me simplify this query. I need to fetch a parameter used in a promise. Here's the gist of the code: function foo(param) { return fromPromise(blabla, blabla2, param) .then((res) => { return res }).catch((error) => { console.log( ...

Parsing JavaScript within HTML using HTML DOM Parser is essential for understanding and manipulating dynamic web content

Currently, I am attempting to extract the download link for zippyshare files in PHP. The challenge I am facing is the need to access their JavaScript code, which I am struggling to do. This is the specific section of the page that I am trying to extract: ...

Populating a div with letter-spacing

I'm facing a challenge in populating a div with text using letter-spacing. The issue at hand is that I am unsure of the width of the div. Initially, my thoughts leaned towards using text-align= justify, however, I found myself lost in the maze withou ...

Conditional types in Typescript that rely on their presence or absence in a dictionary

I am currently attempting to write a function with a complex type signature. It requires two dictionaries, args and funcs. The funcs dictionary maps keys to functions that transform type A to B, while the args dictionary maps a superset of those keys to ei ...

"Enhanced Laravel application incorporating Vue.js for data manipulation, utilizing Axios for API interactions

Lately, I've been diving into the world of Laravel and Vue.js. However, I've run into a roadblock while working with server-side interactions. After making a post request to the server and getting back a response, I noticed that there is an extra ...

The ChartistJs is unable to find the property '_index' of an undefined value, either being 0 or null

I recently incorporated chartistJS to display charts on my website. I have successfully implemented the 'onClick' function provided by chartist. Here is a snippet of the code I used: public horizontalBarchartoptions = { 'onClick&apo ...

What is the best method for extracting various columns from a two-dimensional array?

Can you provide me with some assistance in JavaScript on how to extract multiple columns from a two-dimensional array similar to the example below? Array = [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12], ]; I have come across methods like forEach or map ...

Transmitting an array through Socket.IO using the emit() method

I am currently developing an array in my socket io server and then transmitting it to the client. var roomList = io.sockets.manager.rooms; // creating a new Array to store the clients per room var clientsPerRoom = new Array(); //for (var i ...

Tips for dynamically updating the id value while iterating through a class

Here's the HTML snippet I am working with: <div class="info"> <div class="form-group"> <label class="control-label col-sm-2">Title</label> <div class="col-xs-10 col-sm-8"> <inpu ...

What is the process to designate a specific value to a key in an array?

I need assistance in updating the description array within the schema by adding the about and link values, followed by using the .save() function to store it in the database. Any guidance on this issue would be greatly appreciated. Thank you for your help. ...

Error encountered: index.html:17 - An InvalidStateError was thrown when attempting to execute the 'send' function on the XMLHttpRequest object. The object's state must be OPENED in order to send the Ajax request

When attempting to run index.html on a local host with xampp, an error is encountered: index.html:17 Uncaught InvalidStateError: Failed to execute 'send' on 'XMLHttpRequest': The object's state must be OPENED.sendAjax @ index.html: ...

Ways to display the values stored in localStorage in distinct div elements based on certain conditions

https://i.sstatic.net/BG3qd.png In my recent project, I have created a feature where users can input specific values through input fields and have them stored directly in localStorage. These values represent different tasks, such as Task: Work Description ...

Instructions on how to automatically update the label of a <v-file-input> with the value selected from a <v-select> in Vuetify

As a novice, I am currently working on creating a Vuetify form that includes three <v-file-input> elements, each with a corresponding <v-select> option. My goal is to update the label of each <v-file-input> with the selected value from it ...

Deactivate the dependent picklist functionality on a Visualforce page

After successfully disabling all input and select elements on my page using the following code: $('input').prop('disabled',true); $('select').prop('disabled',true); I encountered an issue with two picklist fields i ...

Using Vue.js to bind items objects in an array with a v-for loop and automatically update them when changes

Utilizing a v-for loop in my component to iterate through an object array at the root level of my app and populate a list. Whenever I use EventBus.$emit in other components to update database tables, it leads to a modification in the $root array data. Is ...

Mastering HTML5 Video: A guide to smoothly playing multiple videos in a loop fashion

Although similar questions have been raised in the past, none has successfully addressed this particular issue; they only provide partial solutions. The objective is as follows: We possess a collection of videos referred to as video1, video2, video3, vid ...

Can you provide any methods for enhancing a pre-existing jQuery event handler?

I have developed a code that resets the form and clears it each time the reset event occurs like this: $("form").on("reset", function(event) { event.preventDefault(); $("form").clearForm(); $("#reportGenerated").empty(); }); This code is stor ...