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

Provide users with the option to select a specific destination for saving their file

In the midst of my spring MVC project, I find myself in need of implementing a file path chooser for users. The goal is to allow users to select a specific location where they can save their files, such as C:\testlocation\sublocation... Despite r ...

Is it possible to replace the prototype of an object with a different object?

When an entity is generated, its prototype is established as another entity. Is it possible to alter the prototype of a previously created entity to point to a different object? ...

Push information into MongoDB without the need to make changes to the entire entry within the MEAN stack

I have a MEAN Stack single-page application up and running for managing exams. The schema/model I have for an exam (or Klausur in German) looks like this: var KlausurSchema = new Schema( { name: String, semester: String, krankm ...

Obtaining the value of dynamically generated elements using JavaScript within PHP scripting

Using JavaScript, I dynamically created some textboxes. Below is the JavaScript code: $(document).on('click','#AddHaContactNumberButton',function(e){ e.preventDefault(); var outerDiv = document.getElementById("HaContactDiv"); var textb ...

Ways to extract information from JSON files

Currently, I am working on a script to extract viewer count and follower count data from Twitch. While I have successfully retrieved the viewer count information, I am encountering issues with extracting the follower count. The essential information can be ...

Converting Vue HTML to PDF without relying on html2canvas functionality

My current challenge involves creating a PDF file from either HTML or Vue component. I have experimented with various libraries such as jsPDF, html2pdf, and vue-html2pdf, but it seems like they all rely on html2canvas, causing the UI to freeze for a few ...

Change elements in real-time

I have a requirement to adjust elements with the class .super-elem by adding an attribute adjusted="true". Initially, I can easily achieve this within the document's ready event : $(".super-elem").attr("adjusted", "true"); However, I may add more . ...

Error: Unable to access the 'wsname' property of an undefined value

I am attempting to retrieve values from a database using the code below (login.js) $.post("http://awebsite.com/app/login.php",{ rep1: rep, password1:password}, function(data) { if(data=='Invalid rep.......') { $('input[type="text"]').c ...

How to efficiently transfer data between PHP and Javascript using ajax?

Struggling greatly with the ajax function within the jQuery library. As a beginner in jQuery, ajax, and php, I am currently engaged in a school project that involves creating a game-like environment where a 10x10 table generates numbers, selects a cell aut ...

The AJAX function is failing to return false

I found this code snippet in my index.html file: <form> <input onkeyup="firstnamecheck(this.value)" type="text" name="firstname" id="Start" class="Inputs" /> <button type="submit" name="Murad" id="tester" title="Register">Register</b ...

Retrieve information from the script tag

Is there a method to extract data from a <script> tag, specifically targeting the 30-minute table on AEMO's website (AEMO Website)? In order to access the data table, I attempted to scrape it but encountered an issue where the button and table ...

Can a "fragile export" be generated in TypeScript?

Testing modular code can be challenging when you have to export things just for the purpose of testing, which can clutter your code and diminish the effectiveness of features like "unused variable" flags on compilers or linters. Even if you remove a usage ...

AngularJS written plugin in Javascript

I developed an app using Rails and AngularJS. A startup has reached out to me about transferring the technology to their website, but they have limited tech resources. The goal is to create a seamless integration process. The idea is to make it similar to ...

The issue with the <Button> component not properly linking in next.js

Having trouble with a button wrapped inside a custom Navbar component. The code snippet in question is: <NavBtn> <Link href="/contact" passHref> <Button> Contact Me </Button> </Link> & ...

Using Vue router to dynamically define the query key when calling router.push()

Hello, I am relatively new to using Vue and have been working on a project that involves making GET requests based on the current URL. In one of my functions, I am trying to dynamically set the value of filterType in the query key within the router.push() ...

Sending all form input data to Ajax for GET request

Today, my goal is to iterate through each textbox with an ID of setting_value, set its name and value to a key-value array, and then send that data in an Ajax request. The problem I'm facing is that it only seems to be inspecting the first instance o ...

Using Material-UI HOC with props传递

One of my custom components is calling another custom component created using withStyles. Here is the code snippet: import {FormControl, InputBase, InputLabel, withStyles} from "@material-ui/core"; import React from "react"; export const CustomInput = wit ...

Converting large numbers (exceeding 53 bits) into a string using JavaScript

I have a REST service that returns JSON. One of the properties in the JSON contains a very large integer, and I need to retrieve it as a string before Javascript messes it up. Is there a way to do this? I attempted to intercept every response using Angular ...

A guide on validating dates in Angular Ionic5 with the help of TypeScript

I have tried multiple solutions, but none seem to work when validating the current date with the date entered by the user. The date is passed from the user into the function parameters, but how do I perform validation? How can I validate the date? isToday( ...

Steps to fix ESlint warning: Avoid using assignment in return Statement (no-return-assign)

In my React application, I am utilizing the package found at https://github.com/appleboy/react-recaptcha to create a Recaptcha component. Below is an image of what the component looks like, along with an eslint warning: The definition of this.recaptchaRe ...