Sending the name of a nested object as a prop to a component

I am facing an issue with passing a deep object from parent to child components in Vue.js. I have created a component for my forms where I pass the main object as props along with a JSON containing each form input's data. The challenge arises when trying to integrate v-model with nested properties. Here is how it looks:

**parent**: <custom-input :personInfo="personInfo" :items="items"> </custom-input>

**child**: <input v-model="personInfo[propertyName]"/>


items: [
    {property: 'birth.date', ...}
]

personInfo : {
    name: '',
    birth: {
        date: ''
    }
}

When trying to set the v-model for 'birth.date', I encounter an error. How can I correctly pass and access this deep object in both the parent and child component?

To better understand the issue, here is a link to a code sandbox showcasing the problem: Codesandbox

Answer №1

Initially, I noticed that you are modifying a prop from the child component, which is considered a detrimental practice. It would be advisable to utilize v-model on the parent component or emit the property and value needing alteration from the child to the parent component.

Regarding your inquiry about 'birth.date', attempting to pass it to v-model proves problematic since JavaScript does not recognize this string as a nested object. Thus, accessing parsedInfo['birth.date'] will search for an exact match within the parsedInfo object rather than treating it as a deep object.

(For instance, if parsed info were structured as:

parsedInfo = { 'birth.date': '' }
, a value could have been located)

To simplify achieving your objective, employing lodash's _.set(obj, property) method appears to be the most straightforward solution. Refer to: https://lodash.com/docs/4.17.15#set

If integrating it with v-model is desired, creating a computed property with get and set methods can be implemented as follows:

infoProperty: {
    get: {
      // return the info[property]
    }
    set: {
      // employ the _.set(obj, property) method
    }
}

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

Iterate through the list retrieved from the backend

I have a list coming from the backend that I need to iterate through and hide a button if any element in the list does not have a status of 6. feedback The response returned can vary in length, not always just one item like this example with 7 elements. ...

The Bootstrap modal window refuses to shut down

In my React application, I am utilizing Bootstrap modal functionality. One specific requirement is that the modal window should remain open when clicking on the modal backdrop. To achieve this, I have implemented the following code: $(".modal").modal({"ba ...

I seem to be stuck on the Pokemon Damage Calculator kata on codewars. I've been trying to pass it, but I

My function seems to be working well, but I'm having trouble passing all the tests. Can anyone offer some assistance? You can find the kata at this link: function calculateDamage(yourType, opponentType, attack, defense) { let key = yourType + opp ...

Using Vue.js to showcase Unicode, hexadecimal emojis, and octal literals in HTML

Received this response from the webserver: "\ud83d\ude48\ud83d\ude02\ud83d\ude30\ud83d\ude09\ud83d\udc4f\ud83c\udffd\ud83d\udc4c\ud83c\udffd\ud83d\udd1d\u2714&b ...

Bundling and minifying Angular2 assets

In the world of ASP.NET (or gulp), bundling and minification are taken care of. However, a different issue arises when following Angular2 tutorials: the view HTML is typically embedded within the component itself. Fortunately, there is a way to separate th ...

Creating an HTML Canvas using an AngularJS template

I am facing an issue with rendering html elements on a canvas using html2canvas JS. I am utilizing AngularJS for data binding on the html page, but unfortunately, the dynamic data is not showing up on the canvas generated from these html elements. For inst ...

Where can I find the @types for a specific lodash package?

Seeking to utilize a specific function from lodash - assignin. I have successfully installed lodash.assignin and incorporated it into my project: import assignIn = require('lodash.assignin'); However, when compiling, an error occurs: "error TS2 ...

Troubleshooting Issue with Angular's ng-view Implementation

I have been following a tutorial on setting up a MEAN stack single-page application with the URL provided. However, after setting up the application, the views are not displaying in the ng-view div as expected. Here is the structure of my app: - app ---- ...

Utilizing a dropdown selection value to filter a list in JavaScript

I have an array of objects where I need to filter based on a dropdown selection. const itemsList=[{ "id":"123", "problems":{ "causes":[ { "SSEnabled": true, "IndusEnabled": false, "LogEnabled": true } ] } ...

Using AJAX to retrieve additional JavaScript code or functions from the server

It's common knowledge that AJAX is often utilized to request a web part in HTML format from the server. However, is it feasible to use AJAX to request a script that includes functions? ...

Exploring ways to access iPhone contacts using HTML5 and JavaScript

How can contacts be fetched on an iPhone with user permission using HTML5 and JavaScript? I understand that an app can access that data, but can a browser do the same? ...

Implement a delay for updating the style of a button by using the setTimeout function

Is it possible to create a button that, when clicked, changes color to green and then reverts back to its original style after 3 seconds? I am able to change the button color using an onClick event in my script. However, I encounter scope errors when tryi ...

Creating a JavaScript interface for an XML API generated by Rails?

Working with a large Ruby on Rails website has been made easier thanks to the REST support in Rails 2. The site's business logic can now be accessed through a consistent XML API. My goal now is to create one or more JavaScript frontends that can inter ...

What causes the variation in output when utilizing React's setState() function?

I'm puzzled by this Whenever I try this.setState({count: count+1}), it only updates the count once no matter how many times I click But when I attempt this.setState({count: this.setState.count}), every click successfully updates the cou ...

Visualizing Data with d3.js Radar Charts Featuring Image Labels

After following a tutorial on creating a d3.js radar chart from http://bl.ocks.org/nbremer/21746a9668ffdf6d8242, I attempted to customize it by adding images to the labels. However, I encountered an issue with aligning the images properly. You can view my ...

Store the active tab in AngularJS with Bootstrap to easily remember and display

After creating a basic AngularJS application with the Bootstrap directive, I noticed that some of my pages have tabs. The issue arises when I am on a tab other than the first one and click a link to navigate to another view. Upon returning (using either th ...

Ways to generate data following the integration of Firebase Firestore in Vue.JS

How can I display the orders data retrieved from Firebase in my browser console? See the image link below for reference. https://i.sstatic.net/HPlaC.png This is the code snippet for fetching data from Firebase and displaying it in the console: orders(){ ...

Prevent UI from updating while backend calls are being made in Vuejs

Imagine a scenario where a user in my application can make multiple backend calls before the first one is even finished. How can I ensure that the User Interface freezes after each call until the backend operation completes in Vuejs? Any suggestions woul ...

Retrieving every piece of information from Kendo Grid's data source

After following a tutorial on exporting Kendo Grid Data, I am now attempting to export all data, not just the data shown on the page. How can I accomplish this task? I attempted to change the page size before retrieving the data: grid.dataSource.pageSize ...

What is the method to spin an item in three js while keeping its axis in focus?

Looking to rotate a globe object around its y-axis smoothly? I have come across a helpful function for achieving this: function rotateAroundObjectAxis(object, axis, radians) { var rotationMatrix = new THREE.Matrix4(); rotationMatrix.makeRotationAxis ...