Troubleshooting: Issue with binding nested data property using bracket access in Vue3 v-model

Having an issue in Vue3 where I am unable to bind a nested property to v-model correctly.

Check out the source code below:

Template:

<div id="app">
  <span>{{level1.level2.level3}}</span> <br/>
  <span>{{level1['level2']['level3']}}</span> <br/>
  <input v-model="level1.level2.level3" /> <br/>
  <input v-model="level1.level2['level3']" /> <br/>
  <input v-model="level1['level2']['level3']" />
</div>

JS

Vue.createApp({
  data() {
    return {
      level1: {
        level2: {
          level3: "val"
        }
      }
    }
  },
  methods: {}
}).mount("#app")

<input v-model="level1.level2.level3" />
binding works

<input v-model="level1.level2['level3']" />
binding works

but

<input v-model="level1['level2']['level3']" />
binding fails

The console warning displayed is

"[Vue warn]: Template compilation error: v-model value must be a valid JavaScript member expression.

Any ideas why level1['level2']['level3'] isn't binding to v-model correctly? To me, it seems like a valid js expression.

Here is the online fiddle for reference.

Answer №1

It appears that the issue lies within Vue itself, rather than in your code. Recent developments indicate that this problem has been resolved in a commit made just over a week ago.

If you are using Vue <=3.1.1 (any version other than the current GitHub master branch), your best course of action would be to adjust how you access properties, wait for an update from Vue, or opt to rely directly on the GitHub repository's master branch (although this may not be advisable unless absolutely necessary and urgent).

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 height of a DIV element can vary based on

Looking at this div structure: DIV3 has a fixed height. The nested DIV5 will be receiving content from Ajax, causing its height to change. Additionally, there are some DHTML elements inside it that also affect the height. DIV5 has a fixed min-height set. ...

Update the content inside a <p> tag dynamically using Javascript based on the selected option

Struggling with Javascript and need some guidance. I have a select box with 4 options, and I want to update the contents of a <p> tag with an id of pricedesc based on the selected option. Here is my current code: function priceText(sel) { var l ...

Issues with JQuery .attr method not functioning as expected

I'm having trouble with the .attr() function in jQuery. It doesn't seem to be changing the background-color of the div with the id "outline" like I expected. Here's an example of my code: <div id="outline"></div> And here is t ...

What is the best way to enforce a required selection from one of the toggle buttons in a React form?

Is there a way to require the user to click one of the toggle buttons in a react form? I need to display an error message if the user submits the form without selecting a button. I tried using the "required" attribute in the form but it didn't work. H ...

Merge the chosen values from the drop-down menu into one string

Any suggestions would be greatly appreciated! I am currently developing an application using ASP.NET web forms that consists of a dropdown list and two list boxes. I want these elements to be cloned whenever a specific button is clicked. However, my issue ...

Obtaining IDs of Divs that have been Dragged into a Drop Zone upon Clicking a Button using JavaScript

I'm currently working on a solution to extract the ids of dragged divs once they have been placed in the drop zone. Each drag component is assigned an id ranging from drag1 through drag8, while the drop zone is labeled as div drop zone. Since there a ...

The image has been restricted from view due to CORS specifically on Windows operating systems

This Vue component includes a photo block and an "edit" button. <template> <div> <tui-image-editor ref="editor" > </tui-image-editor> <div class=""> <img :src="img&qu ...

Input of data and salt must be provided

(node:35) UnhandledPromiseRejectionWarning: Error: data and salt arguments required. Can someone please assist me in resolving this error that I am encountering? const validatePassword = (password) => { return password.length > 4; }; app.post("/r ...

Issue when Updating Component State: React child cannot be an object

I'm currently in the process of familiarizing myself with React and how to manage component state. My goal is to build a component, set up the initial state using a constructor, make a GET request to the server to fetch an array of objects, and then ...

What could be causing Vue to cease functioning after rendering two pages in a cshtml file?

Coming from a very beginner level in programming, I am currently working on developing a website using asp.net MVC. However, after rendering a view page to _layout, the vue in the _layout suddenly stopped working and an error message appeared in the conso ...

Data retrieval error, function returned instead of expected value

Hey everyone, I'm currently working on fetching data using the GET method and I would like the data to be displayed after clicking a button, following standard CRUD operations. As a beginner in programming, I could use some help. Any assistance is gre ...

Why isn't my content appearing correctly on different pages?

This ecommerce site is my very first project using React. I have created pages for Contact, Login, and more. The footer and other components display correctly on the home page, but when navigating to other pages, the content appears shortened. For referen ...

Create custom PDF documents on-the-fly using data from JSON with Electron-Vue and easily download them

I am looking to utilize electron in conjunction with vuejs in order to dynamically create PDF files for download on the fly. Essentially, the user scenario is as follows: - when a user clicks on the download PDF button, data will be fetched as JSON and t ...

Discover the Primevue DataTable feature that enables dynamic column and column grouping functionality, with the added bonus of gridlines disappearing as you scroll down

I am currently utilizing the PrimeVue DataTable feature with dynamic column and column grouping. Initially, when the table loads, everything appears to be great - gridlines are visible, columns freeze, scrollable functionality is working as expected. htt ...

What is the best way to populate all data in select2 (4.0) upon page load?

I'm currently utilizing the select2 plugin (v.4.0) and have a specific goal in mind: $("#search-input-chains").select2({ placeholder: "Unit", theme: "bootstrap4", ...

Use AJAX to send a form submission along with an anchor tag

I've been facing a challenge trying to make this work, but unfortunately, I haven't had any success. Typically, all my forms include a submit input, and I handle AJAX submission in the following manner: <script> $("input#submit").click(fun ...

Jquery code not responding to ajax callback

i have the following two functions defined: function populateTableData(){ jQuery('select[name="drg-select"]').change(function() { drgValue=jQuery(this).val(); url="http://localhost/ur ...

Unexpected request causes a dilemma during the karma test for an Angular directive

Let's discuss a straightforward directive: 'use strict'; angular .module('app') .directive('ngEmailMask', ngEmailMask); function ngEmailMask() { var directive = { replace: true, restrict: 'EA', ...

JavaScript: Changing the names of all object keys

As a beginner, I am struggling to rename some objects in my key using a map function. Below is my current array: "platforms": [ { "id": 6, "name": "PC (Microsoft Windows)" }, { "id": 11, "na ...

Using multiple jQuery dialogs on index.php

I have a vision for my website to mirror the Windows environment, complete with icons that prompt dialog boxes when clicked. On my site's index page, I've added the following code within the head tags: <link rel="stylesheet" href="http://cod ...