Creating a distinctive vue form component from scratch

I have a requirement to develop a Vue component that enables users to create or edit a mailing address. The existing component structure is as follows:

<template>
<v-container>
  <v-form ref="form" lazy-validation>
    <v-text-field 
      v-bind:value="address.street"
      v-on:input="$emit('input', $event.target.value)"
      label="Number and Street">
    </v-text-field>
  </v-form>
</v-container>
</template>

<script>    
export default {
  name: 'address-form',
  props: ['address'],
  // other stuff
}
</script>

The parent component receives a large object from the store and passes the address as a prop like this:

<p>Just for testing: {{outerObject.address.street}}</p>
<address-form address="outerObject.address" v-on:changed-address="changedAddress" />

Within the script...

computed: {
  outerObject () {
    let id = this.$route.query.id
    let outerObject = this.outerObjects.find(o => o.id === id)
    return (outerObject) ? outerObject : this.newOuterObject
  },
  ...mapGetters(['outerObjects'])

However, I am facing two issues:

(1) Despite initializing outerObject correctly in the parent component (where outerObject.address.street is visible), the form's street input does not display an initial value.

(2) When typing into the form input, I encounter the following error message:

[Vue warn]: Error in v-on handler: "TypeError: Cannot read property 'value' of undefined"

The issue seems to be related to the use of 'value' in reference to the input. My understanding is that value is a property of both the input and $event.target. I have tried modifying 'value' to 'street' and different variations, but none have been successful so far.

My objective is to establish "two-way binding" between the form inputs and the outerObject on the parent component so that any edits made by the user reflect in the outerObject. Could you please point out what might be incorrect?

Answer №1

Implementing two-way binding involves the following steps:

  1. Include value in your list of props.
  2. Add v-on:change to the input field within your component.
  3. When a change occurs, emit it using this.$emit('input', newValue).

Now you can utilize your component like this:

<address-comp v-model='outerObject.address'></address-comp>

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

Failed attempt to perform Ajax requests for REST API data

I am currently working on developing an application that requires a login through a REST API to retrieve a session id. To achieve this, I have set up a button that triggers a JavaScript function for making an AJAX call to authenticate the user. The result ...

When the Ionic framework is initialized, it automatically redirects users to the home

I'm currently developing a mobile app using Ionic, and I've encountered an unusual issue. Whenever I refresh the page (pressing F5) from a specific route like "/tabs/connected/channel/edit", I always get redirected to "/tabs/home" after the state ...

Exploring the process of performing an AJAX JQuery HTTP request using JavaScript and PHP on the server side - any tips?

Greetings! I have developed a web application using HTML, CSS, and JavaScript. To enhance functionality, I have integrated Bootstrap and jQuery into the project. The application comprises both client-side and server-side components. Let's take a look ...

Building a hierarchical object structure from an array

I am working with an array of objects that looks like this: const sorted = [ { IsoCode: "EUR", Buy: 1.948, Sell: 1.963 }, { IsoCode: "GBP", Buy: 2.1184, Sell: 2.1894 }, { IsoCode: "USD", Buy: 1.5781, Sell: 1.6484 }, ] and my ...

Choose the parent element along with its sibling elements

How can I target not only an element's siblings but also its parent itself? The .parent().siblings() method does not include the original element's parent in the selection. $(this).parent().addClass("active").siblings().removeClass("active"); I ...

Effective methods for synchronizing two dropdown menus with varied displays using React

Having a list of countries with specific key-value pairs, I am interested in creating two Dropdown lists. The first will display the keys, while the second will show the corresponding text values. The main objective is to provide the option to select eith ...

Using a PHP switch case statement with an HTML multiple select dropdown

I am facing an issue with my HTML multiple select box: <option value="1">First choice</option> <option value="2">Second choice</option> <option value="3">Third choice</option> Using jQuery, ...

Refresh a page automatically upon pressing the back button in Angular

I am currently working on an Angular 8 application with over 100 pages (components) that is specifically designed for the Chrome browser. However, I have encountered an issue where the CSS randomly gets distorted when I click the browser's back button ...

Creating a fresh shortcut on the selenium IDE

Is there a way to customize shortcuts in Selenium IDE by modifying its code? For instance, I would like to set the shortcut ctrl + p for the action run test case, similar to how the save action is assigned ctrl + s. I've searched for the JavaScript ...

transferring values/properties to child component within a customized component

I am curious about the most efficient method for passing numerous props to a component that is enclosed within an external component. For instance: code <template> <div> <v-text-field :error="summonerNameError" ...

Eliminate the blank choice from X-editable Select

Lately, I've been dealing with AngularJS and X-editable and one thing that has been troubling me is the fact that the select option always includes an empty first child. Here's the HTML <span editable-select="curuser.roles" e-name="roles" ...

Customizing the date format in Vuetify's Datepicker

I am currently using a Vuetify Datepicker component in my project: <v-menu v-model="menu1" :close-on-content-click="false" max-width="290" > <template v-slot:activator="{ on }"> <v-text-field v-model="editedItem. ...

Is the concept of client-side authentication, similar to that on single-page applications, simply a facade to deter users who trust in it from tampering with the data on the server side?

Is client-side authentication, like on SPAs, simply a ruse to deter users who believe it from attempting to manipulate the data on the backend, which is protected by real authentication? When we develop SPAs with technologies such as Vue, Vue Router, Reac ...

leveraging jQuery mobile for asynchronous requests

I've been attempting to print a jQuery mobile element using ajax, but I'm running into an issue where the result isn't being encoded as jQuery mobile is intended to do. Below is a simplified excerpt of the JavaScript code responsible for t ...

Inform the user that an error has occurred when attempting to perform an invalid

While using redux promise middleware for my front end, I am wondering about the correct status code to throw from my backend in case of an error. I know that I can use res.status(500).json(something), but is 500 the appropriate code for all types of erro ...

Having difficulties with vue installation

Whenever I take a break from using certain packages, I typically opt for fresh re-installs. However, when I recently attempted to do a fresh install of Vue.js and vue-cli by running vue init on a new project, I encountered an error stating that Vue was not ...

User form not triggering post requests

I have a unique react blog application embedded with a form for submitting intriguing blog posts. The setup includes a server, routes, model, and controllers for fetch requests. Surprisingly, everything functions impeccably when tested on Postman. However, ...

What causes my slider to speed up with an increase in items and slow down with fewer items in bxslider?

Find more information here jQuery('.homepage_slider').bxSlider( { minSlides: 1, maxSlides: 4, slideWidth: 200, slideMargin: 30, ...

Can an identification be included in a label element?

My inquiry is as follows: <label for="gender" class="error">Choose</label> I am interested in dynamically adding an id attribute to the above line using jQuery or JavaScript, resulting in the following html: <label for="gender" class="err ...

Implementing optimal techniques to create a JavaScript file for data retrieval in a Node.js application

I've developed a file specifically for data access purposes, where I'm keeping all my functions: const sql = require('mssql'); async function getUsers(config) { try { let pool = await sql.connect(conf ...