The profound responsiveness of properties in Vue.js components

I am working on creating a basic page builder that includes row elements, their child column elements, and finally the component that needs to be called. To accomplish this, I have designed an architecture where the dataset is defined in the root component and the data is passed to its child elements using props. Let's consider a root component:

<template>
    <div>
        <button @click.prevent="addRowField"></button>
        <row-element v-if="elements.length" v-for="(row, index) in elements" :key="'row_index_'+index" :attrs="row.attrs" :child_components="row.child_components" :row_index="index"></row-element>
    </div>
</template>

<script>
    import RowElement from "../../Components/Builder/RowElement";

    export default {
        name: "edit",
        data(){
            return{
                elements: [],
            }
        },
        created() {
            this.listenToEvents();
        },
        components: {
            RowElement,
        },
        methods:{
            addRowField() {
                const row_element = {
                    component: 'row',
                    attrs: {},
                    child_components: [

                    ]
                }
                this.elements.push(row_element)
            }
        },
    }
</script>

In this code snippet, there is a button used to push the element, and its elements are passed to its child elements through props. The RowElement component has the following code:

<template>
    <div>
        <column-element v-if="child_components.length" v-for="(column,index) in child_components" :key="'element_index_'+index"  :attrs="column.attrs" :child_components="column.child_components" :row_index="row_index" :column_index="index"></column-element>
    </div>
    <button @click="addColumn"></button>
</template>

<script>
    import ColumnElement from "./ColumnElement";
    export default {
        name: "RowElement",
        components: {ColumnElement},
        props: {
            attrs: Object,
            child_components: Array,
            row_index: Number
        },
        methods:{
            addColumn(type, index) {
               // Logic here then emit event for parent element to push columns

                eventBus.$emit('add-columns', {column: column_element, index: index});
            }
        }
    }
</script>

Answer №1

The problem lies in how you are updating your data within the addComponent method.

Vue is unable to detect changes when you directly modify an array by its index, such as,

arr[0] = 10

As explained in their guide on detecting array changes,

Vue wraps mutation methods of observed arrays to trigger view updates. The covered methods include:

  1. push()
  2. pop()
  3. shift()
  4. unshift()
  5. splice()
  6. sort()
  7. reverse()

Instead of direct modification, you should do this:

this.elements[data.row_index].child_components[data.column_index].child_components.splice(data.element_index, 1, data.component);

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

Utilizing JQuery mobile for a seamless user experience, our website features only one secure HTTPS

It's a known issue that JQuery mobile struggles with redirects from the server. What could be the most effective approach to creating just one secure page on a website? Consider this scenario: The user starts at the root of the site -> The user ...

Custom styling options for Google Chart

I have been attempting to dynamically adjust the width and height of a google-chart directive by utilizing input field values. However, I am encountering an issue where the width and height are not updating as expected. Check out the Plunker here $scope. ...

Ejs does not automatically include the message when returning a template to render in HTML

I need help rewording the title properly. I am facing an issue in my server code where a database query is executed to check if the account name already exists. If it does, a specific message should be displayed using this template: con.query('SELECT ...

Exploring custom JavaScript with Construct 2's generated code

How can I access the score of a game user in Construct2 Engine using external JavaScript? Is there a method to achieve this? Appreciate any guidance on this matter. ...

What is the best way to fetch images from a JSON object in a React application?

I have been trying to load images from an array of objects in React, but I keep encountering a "module not found" error. This issue has been frustrating me for the past couple of days. Can someone please help me troubleshoot this problem or provide some su ...

When a user toggles one div, the other toggled div should automatically close

Below is a snippet of code where I have implemented a functionality using vue.js to toggle different divs based on button clicks. The current setup allows for toggling individual divs when their corresponding buttons are clicked. However, I am looking to ...

Selectize Fails to Populate Options Using Ajax

ExpressJS is the platform I am using to develop a management dashboard for a community project. Right now, my main concern is integrating a modal that allows users to add new games to a database. Although I am able to fetch data remotely, I am facing diffi ...

Delay with Vue.js v-bind causing form submission to occur before the value is updated

Trying to update a hidden input with a value from a SweetAlert modal, but encountering issues. The following code isn't working as expected - the form submits, but the hidden field's value remains null. HTML: <input type="hidden" name="inpu ...

Struggling to move forward with developing a task management app due to a roadblock

I am seeking guidance rather than a direct answer, as this is crucial for my final exam. I require a starting point to help me fill in the missing pieces. It's important to note that local storage cannot be used to store user input. // POST handler ad ...

Vue.js is experiencing difficulties in rendering the project

After trying to install a Vue.js package, I encountered errors which prompted me to remove it. However, even after removal, I am still facing difficulties serving my project. Here is the log for reference: Click here ...

"Unexpectedly, JavaScript on Android ceases to function without any

Experiencing a frustrating issue with my application that involves a WebView and a html-page with JavaScript functions. Occasionally, the JavaScript functions fail to execute randomly. Despite spending the entire day trying to debug this issue, I can only ...

What could be causing a parse error and missing authorization token in an AJAX request?

I recently wrote some code to connect a chat bot to Viber using the REST API. The main part of the code looks like this -: $.ajax({ url : url , dataType : "jsonp", type : 'POST', jsonpCallback: 'fn', headers: { 'X-Viber-Auth- ...

Finding the index of specific rows in an iview vue Table

Is there a way to retrieve the index of selected rows in a Table component within an iview vue framework? For instance, let's consider the following table structure: <Table ref="selection" :columns="columns4" :data="data1" @on-selection-change="up ...

Testing form submission in React with React Testing Library and checking if prop is called

Feel free to check out the issue in action using this codesandbox link: https://codesandbox.io/s/l5835jo1rm To illustrate, I've created a component that fetches a random image every time a button is clicked within the form: import { Button } from " ...

Enhance the language with react-intl and MobX State Tree integration

Currently, I am integrating react-intl with Mobx state tree for the first time. Within my project, there are two buttons located in the header section - one for 'it' and the other for 'en'. Upon clicking these buttons, the selected lan ...

How can we add up the sum with just one click and then display the

Is there a way to calculate numbers within specific DIV boxes and display the total in a separate DIV when a box is clicked? I've attempted different methods, including enclosing the code within the window.onclick function. Check out my code on this J ...

Switching between TWO classes in Vuejs

I've been grappling with how to add or remove TWO classes in Vue JS for hours. The documentation examples only demonstrate toggling one class. My goal is to change a button's class to either: active toggle-on or toggle-off when clicked. While ...

Redux: triggering a dispatch when a component or function is initialized

I'm facing a challenge where I need to update a state only when a specific functional component is initialized. My initial approach was to try something like this: export default function SalesFeedPage(){ const {salesFeed} = useSelector((state) => ...

Concealing photos only to reveal them in an elegant fancybox display

I am looking to create a hidden gallery that can be displayed in fancybox. The idea is for visitors to click on one image and then have the ability to view more "hidden" pictures within fancybox. I know this can be accomplished by showing links or images ...

Is it possible to combine the outcomes of a SQL query that produces numerous entities with identical identifiers?

Currently working on my first Node.js API project. Within the '/posts' endpoint, I am receiving data in this format: [ { "POST_ID": 1, "POST_TITLE": "Post N.1", "POST_DESCRIPTION" ...