Utilizing Vue's Vue.set method on an object containing numerous nested objects

I'm currently experimenting with Vue.set() to update a state object in Vue 2.

This is the structure of the object:

state: {
    entries: [
        // entry 1
        fields: {
            someProperties : ''
            // I want to add another property called 'googleInfos'
        }
    ], [
        // entry 2
        fields: {
            someProperties : ''
            // I want to add another property called 'googleInfos'

        }
    ]
}

Previously, I used this mutation to update it. Each entry is updated individually due to varying content:

ADD_GOOGLE_INFOS (state, {index, infos}) {
    state.entries[index].fields.googleInfos = infos
}

Now, I am attempting to implement Vue.set() to prevent change detection issues.

The syntax for Vue.set() is as follows:

Vue.set(state.object, key, value)

I tried using this approach, but it doesn't seem to be effective as state.entries[index] is not a top-level object:

Vue.set(state.entries[index], 'fields.googleInfos', infos)

Another attempt that failed was:

Vue.set(state.entries, '[index].fields.googleInfos', infos)

Could anyone provide insight into what might be incorrect here?

Answer №1

The one element that remains unreactive is the additional property you attempt to insert into the fields object.

To include the googleInfos property, you need to define it in the mutation as follows:

ADD_GOOGLE_INFOS (state, {index, infos}) {
    Vue.set(state.entries[index].fields, 'googleInfos', infos);
}

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 getElementById method in JavaScript can result in a null return value

Why is null returned by the getElementById method in JavaScript? <html> <head> <title>test_elementObject</title> <script language="JavaScript" type="text/javascript"> <!-- var input1 = document.getElementById ( " ...

unable to access the object3D within the current scene

Currently facing an issue where I am unable to retrieve Object3D from the scene, despite the mesh objects being displayed within the scene. Strangely, the scene.children array does not reflect this. Take a look at the screenshot here Here is the code sni ...

Are there any CSS hacks available to address the combination of position: sticky and overflow within the parent element?

I've encountered a sticky position issue when the overflow property is set to auto on a parent element. I've tried various solutions like adding an extra wrapper or using clip instead of auto, but none have worked. While I did find a solution usi ...

Ways to tweak export data in Vue 3 & beyond

Check out my code snippet below: export default { name: 'form-loading-spinner', data(){ return{ loading: false, form : { phone: "", token: null, }, ...

Encountered a NodeJS error while attempting to locate information in a mongo DB: UnhandledPromiseRejectionWarning

In my MEAN stack application, I am working on implementing a login feature that includes social login functionality. When a new user attempts to log in using Facebook, I need to verify if their Facebook account is already registered in my MongoDB database. ...

How can I change the padding that is being inherited from "@ .MuiAlert-icon"?

Is there a method to override the padding inherited from "@ .MuiAlert-icon"? The Chrome inspector indicates that the current padding is set to 7px. .MuiAlert-icon { display: flex; opacity: 0.9; padding: 7px 0; font-size: 22px; ...

The image fails to display correctly

As I work on creating a basic webpage in HTML and JavaScript, my goal is to validate certain parameters (such as width, height) of an image that users upload via a form. In JavaScript, I extract the file from the form and attempt to display it as an image ...

Avoid displaying incorrect UI states while data is being loaded

Within my Vue application version 2.6.X, I utilize single-file components similar to the following structure: <template> <div> <div v-if="users.length"> <!-- users list rendering here --> </div> & ...

Incorporating JSON data into an HTML table

Looking to populate an HTML table with JSON data, but struggling with parsing and appending the data correctly. Can anyone provide guidance or assistance? <!DOCTYPE html> <html> <head> <title>JSON Demo</title> < ...

The application of document.body.style.backgroundColor is not compatible with an external CSS style sheet

Why does document.body.style.backgroundColor not work with an external CSS style sheet? I have the following CSS: body { background-color: red; } In my css style sheet, when I use JavaScript alert alert(document.body.style.backgroundColor);, it sh ...

The Input element's onChange event is not functioning as expected

I am experiencing some issues with my code. I am trying to dynamically change the background color based on user input, but I am struggling to make it work. It seems like a simple task, so I must be missing something. Below is my HTML markup and jQuery: ...

Ways to acquire Json information from various websites

Imagine having a directory called /api/cat/fact.js. You want to fetch JSON Data from the website catfact.ninja The challenge is that you are unable to use the require() or request() package. If you try using require, it will display an error message say ...

Adding more of a certain product in Laravel using the increment method from a modal window and ajax isn

I'm currently working on developing an inventory system using laravel. I have encountered a problem with updating the quantity of a product after it has been registered. I attempted to use the increment method in laravel, but unfortunately, it did not ...

What are the steps to successfully deploy a static website created with Next.js on Vercel?

Using the Next.js static site generator, I created a simple static site that I now want to deploy on Vercel. However, I keep encountering an error during the build process. While I have successfully deployed this site on other static hosting platforms befo ...

Mapping JSONP responses for jQuery UI autocomplete

Attempting to implement jqueryUI autocomplete. After alerting the response, it shows: ([ { "id": "test", "label": "test", "value": "test" } ]); However, when trying to map the result, the dropdown remains empty. Here is the code being used: <script> ...

Issue encountered in integrating Plotly.js with WebGL into the Vue.js application: WebGL throwing an INVALID_OPERATION error with messages "useProgram: program not valid" and "drawArraysInstancedANGLE"

The current versions I am using are Plotly 2.14.0 (latest) and VueJs 3.2.37 (latest). I am attempting to plot with the type "scattergl", but encountering error messages in Chrome/Edge browsers: WebGL: INVALID_OPERATION: useProgram: program not valid WebGL ...

Ways to modify a link tag based on a specific browser width

Looking for a solution to optimize the drop-down sign in box code that gets appended to a hamburger menu when the mobile width media query is called. Can we refactor this to switch the drop-down sign in box to just an anchor tag (<a href="signin">< ...

Output the initial value and subsequently disregard any values emitted during a specified time interval

Check out my code snippet: app.component.ts notifier$ = new BehaviorSubject<any>({}); notify() { this.notifier$.next({}); } app.component.html <div (scroll)="notify()"></div> <child-component [inp]="notifier$ | async" /> ...

In my VueJs project, I developed a custom button component that includes a loader feature. I initially passed the loader as a prop, but I am currently facing challenges integrating it with a method in another VueJs page

Here is an example of my component: <button class="BtnLoader btn btn-primary-contained btn-iconed btn-mobile btn-important" @click="onClick" :aria-label="label" :title="title"> <i v-if="loader" cla ...

Can you tell me what that sequence of characters is at the conclusion of a styled-component?

Technology Stack: React, Typescript I believe this question may have been asked before, but I am unsure how to search for it... I am interested in creating a wrapper for the styled-components library... Here is the API reference for the specific packag ...