Using TypeScript with Vue to Retrieve Information from a Form

Currently, I am attempting to retrieve data from a form in Vue using TypeScript. However, when declaring the data that I intend to use with this form, it seems to be posted on the fields as shown in this screenshot: message getting posted. I am unsure how to correct this issue because apparently, very few people utilize Vue with TypeScript, making it difficult to find a solution. Below is the code for the component, and I would greatly appreciate any assistance in resolving this problem or suggesting an alternate method of declaring the data.

<form class="container" id="help-form" @submit.prevent="postForm">
        <h1>Help Portal</h1>
        <h3>
            Here you can reach out to GAAP I.A.P advisors to receive digital assistance.
        </h3>

        <div class="section">
            ... (omitted for brevity)
        </div>

        <div class="section">
            ... (omitted for brevity)
        </div>

        <div class="section">
            ... (omitted for brevity)
        </div>

        <div class="section">
            ... (omitted for brevity)
        </div>

        <p>
            *Once you submit your request, a designated advisor from GAAP will contact you promptly to follow up.
        </p>
    </form>

    <script lang="ts">
        import { defineComponent } from "vue";
        
        export default defineComponent({
          name: "HelpForm",
          
          data() {
              return{
                seccion: String,
                nombre: String,
                correo: String,
                telefono: Number,
                mensaje: String,
                
              }
          },
          
          methods: {
              postForm: function () {
                  console.log({seccion: this.seccion, nombre: this.nombre, correo: this.correo, telefono: this.telefono});
              },
          },
        });
    </script>
    

Answer №1

Here's the approach you're taking:

data() {
  return{
    section: String,
    name: String,
    email: String,
    phone: Number,
    message: String, 
   }
  },

You are assigning the String JavaScript object as the initial value for your variables, which is why you see that output in the view.

In my experience, I typically initialize variables with an empty string value, but using "" as string should also work. Personally, I prefer initializing them as empty strings like this:

data() {
  return{
    section: "",
    name: "",
    email: "",
    phone: "",
    message: "", 
   }
  },

When it comes to objects, I do adhere to strong typing principles :)

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

ways to coordinate two subscriptions so that one initiates only when the other one emits

Currently, I am developing an Angular application with a specific scenario. I have an observable signal named dataFetchedEvent$, which indicates that data has been fetched from a remote location. Additionally, there is a form that relies on this remote dat ...

Encounter an Unexpected Token Issue when using NextJS-auth0 in Jest

I am facing a problem with my Next.js app that is integrated with the nextjs-auth0 package. Whenever I attempt to test a particular file and include the following import: import { getSession } from '@auth0/nextjs-auth0'; An error occurs, stating ...

DotLottie file loading issues

While using dotlottie/react-player, webpack 4, and react 16, I encountered module parse failed errors during compilation. "@dotlottie/react-player": "^1.6.5" "webpack": "^4.44.2", "react": "16.14.0&qu ...

Is it possible to dynamically close the parent modal based on input from the child component?

As I follow a tutorial, I am working on importing the stripe function from two js files. The goal is to display my stripe payment in a modal. However, I am unsure how to close the modal once I receive a successful payment message in the child. Below are s ...

Using :global() and custom data attributes to apply styles to dynamically added classes

Currently, I am working on creating a typing game that is reminiscent of monkeytype.com. In this game, every letter is linked to classes that change dynamically from an empty string to either 'correct' or 'incorrect', depending on wheth ...

Uncover the solution to eliminating webpack warnings associated with incorporating the winston logger by utilizing the ContextReplacementPlugin

When running webpack on a project that includes the winston package, several warnings are generated. This is because webpack automatically includes non-javascript files due to a lazy-loading mechanism in a dependency called logform. The issue arises when ...

Component fails to update when attribute is modified

My issue is that the crud-table component does not refresh when I change currentTable. It works when I assign currentTable = 'doctor' in the created() hook, but not here. Why is that? <template> <div id="adminpanel"> <div id ...

The initial click may not gather all the information, but the subsequent click will capture all necessary data

Issue with button logging on second click instead of first, skipping object iteration function. I attempted using promises and async await on functions to solve this issue, but without success. // Button Code const btn = document.querySelector("button") ...

creating pages within a freshly established subdirectory

Hello fellow developers! I could use some advice on an issue I'm facing. After creating a new folder in the pages directory, I am unable to render the pages live. Should I roll back and move all my templates back to the pages folder or is there anothe ...

What is the best way to execute a function on the output of *ngFor directive in Angular 2?

Imagine having a list of all the users within your system: allUsers = { a: {name:'Adam',email:'<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="39585d5854794d5c4a4d5a56175a56... f: {name:'fred' ...

What is the best way to repurpose the vuex module for multiple components?

Currently, I am tackling the pagination aspect of a project that involves handling a large amount of data. My initial instinct was to store this data within Vuex. However, I ended up implementing all the logic in the Vuex store module. Now, my goal is to f ...

What are the steps to properly build and implement a buffer for socket communication?

I have encountered an issue while converting a code snippet to TypeScript, specifically with the use of a Buffer in conjunction with a UDP socket. The original code fragment is as follows: /// <reference path="../node_modules/DefinitelyTyped/node/node ...

Instance of a Component Available Worldwide

When working with Vue 2.x in our production applications, we utilize a toast component. This toast component is mounted once through a plugin (code provided below) and then added to the Vue prototype for easy access in every component instance. This setup ...

Error in Protractor Typescript: The 'By' type does not share any properties with the 'Locator' type

https://i.stack.imgur.com/8j2PR.png All the different versions Error. Protractor version : 5.2.0 npm : 3.10.10 node :6.9.5 typescript :2.6.0 The 'By' type does not share any properties with the 'Locator' type What is the solution to ...

Encountering the issue of receiving "undefined" when utilizing the http .get() method for the first time in Angular 2

When working in Angular, I am attempting to extract data from an endpoint. Below is the service code: export class VideosService { result; constructor(private http: Http, public router: Router) { } getVideos() { this.http.get('http://local ...

Hide selected item in the input field on Treeselect vue.js

Is there a way to hide the items that are selected from being displayed here? Instead, I would like only the placeholder text to show, which in this case is "Select your favourite(s)..." So the end result would appear as follows: ...

Assign a value to a variable using a function in Typescript

Is there a way in typescript to explicitly indicate that a function is responsible for assigning value to a variable? UPDATED CODE Here, the code has been simplified. While getText() ensures that it will never be undefined, this may not hold true in subs ...

Issues with refreshing Laravel Vue components

After installing Laravel 5.4, I created a new component which displayed correctly. Running 'npm run watch' compiled my .vue files successfully. However, any changes made to the .vue files do not reflect the updates. Even after intentionally cr ...

Ways to enhance a component by incorporating default properties in React/TypeScript

I am looking to enhance a React/TS component (specifically the @mui/x-data-grid DataGrid) by populating the classes prop with my own application classes. Initially, I thought about creating a new component called CustomDataGrid like this: import React fro ...

Tips for conducting a worldwide search in Angular 2?

I'm currently navigating my way through angular2 development and I am aiming to conduct a comprehensive search within an array of JSON objects. To illustrate, consider this sample array: invoiceList = [ { invoiceNumber: 1234, invo ...