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

When trying to save a child entity, TypeORM's lazy loading feature fails to update

I've been troubleshooting an issue and trying various methods to resolve it, but I haven't had any success. Hopefully, someone here can assist me. Essentially, I have a one-to-one relationship that needs to be lazy-loaded. The relationship tree ...

The Vue JS Option element requires the "selected" attribute to be utilized as a property instead

I'm attempting to automatically have an option selected if the "selected" property is present on the option object. Here is my code snippet: <template> <select v-model="model" :placeholder="placeholder"> <option v-for="opt ...

Unable to retrieve information from a function in Vue.js (using Ionic framework)

When attempting to extract a variable from a method, I encounter the following error message: Property 'commentLikeVisible' does not exist on type '{ toggleCommentLikeVisible: () => void; This is the code I am working with: <template& ...

Leveraging Shared Modules Component across multiple modules in Angular can enhance code re

In my project structure, I have a shared folder containing shared.module.ts. Additionally, there is a modules folder with sub-modules, one of which is Dashboard.module.ts. Inside the shared module, I created a custom sidebar menu that I intend to use withi ...

Decreased Performance in Vue Threejs with Larger JSON Data Sets

I am currently upgrading a legacy Three.js project from Angular 1.5 to Vue 2.6. The project is used for visualizing objects in JSON file format and I'm experiencing a drop in frame rate, going from ~60FPS in Angular to ~12FPS in Vue when loading large ...

Obtain the appropriate selection in the dropdown based on the model in Angular

I am working on a dropdown menu that contains numbers ranging from 1 to 10. Below is the HTML code for it: <div class="form-group"> <label>{{l("RoomNumber")}}</label> <p-dropdown [disab ...

Vue.js/Bueitify: Simplify search results by utilizing the searchable attribute in the b-table component

Currently, I am utilizing the `buefy` framework to develop a table with input filters embedded in columns. Here is a glimpse of the code: <b-table :data="cars" :sticky-header="true" :selected.sync="select ...

Is it possible to modify the Angular global error handler to accept additional parameters besides just the error object?

After exploring the capabilities of https://angular.io/api/core/ErrorHandler, I have found that it allows me to override the global error handler with an error object. I appreciate how I can easily define my own global error handler and use it wherever nec ...

Exploring ways to destructure the useContext hook with a null default value in your Typescript code

Initially, I set up a context with a null value and now I am trying to access it in another component. However, when I destructure it to retrieve the variables from the context, I encounter a TypeScript error: Property 'users' does not exist on ...

Having trouble reading the length property of undefined in Angular 7

Seeking to obtain a picture link of the object. The objects are stored in an array and the typescript method looks like this: getMealPicture(orderLineMeal: OrderLine): string { for (let meal of this.meals) { if (meal.id === orderLineMeal.mealId) ...

Ignoring TypeScript overloads after the initial overload declaration

Issue An error occurs when attempting to call an overload method for a specific function. The call only works for the first defined overload, causing an error if the call is made to the second overload with mismatched typing compared to the first overload ...

Unlocking the secrets of interacting with OpenLayers map functions in Vue 3

I'm utilizing the vue3-openlyers library to develop a map similar to this code snippet: <ol-map ref="map"> ... </ol-map> While using Vue's composition API, I am attempting to utilize the getSize() method of the map object: ...

Creating a blueprint for an object that inherits from another interface

I am looking to create an interface that includes unknown properties for one object, while also extending it with known properties from another interface. Here is my attempt: public async dispatchMessage(): Promise<{} extends IHasResponseFormat> I ...

Vue and Express will not be able to communicate with each other with the help of NG

I have successfully established a Vue frontend and an Express backend on a DigitalOcean droplet, with NGINX facilitating communication through an API. The frontend is functioning properly; however, when attempting to access an API route, I encounter a "CON ...

Leveraging Vue Data for Storing CSS Properties

I am currently utilizing the Quasar framework in conjunction with Vue for my application development. Below is a snippet of my code: <q-tooltip content-class="bg-amber text-black shadow-4" :offset="[10, 10]"> Save </q-tooltip> <q-tooltip c ...

What advantages does using an RxJS Subject have over handling multiple event listeners individually in terms of speed

After investigating a page's slow performance, I identified an angular directive as the root cause. The culprit was a piece of code that registered event listeners on the window keydown event multiple times: @HostListener('window:keydown', ...

Having trouble selecting all checkboxes in the tree using angular2-tree when it first initializes

My goal is to have all checkboxes auto-checked when clicking the "feed data" button, along with loading the data tree. I've attempted using the following code snippet: this.treeComp.treeModel.doForAll((node: TreeNode) => node.setIsSelected(true)); ...

Create a tuple type that encompasses all possible paths within a recursive object configuration

Consider the following templates for 'business' types, representing compound and atomic states: interface CompoundState<TName extends string, TChildren extends { [key: string]: AnyCompoundState | AnyAtomicState }> { type: 'parent&ap ...

Is there a way to verify if a user taps outside a component in react-native?

I have implemented a custom select feature, but I am facing an issue with closing it when clicking outside the select or options. The "button" is essentially a TouchableOpacity, and upon clicking on it, the list of options appears. Currently, I can only cl ...

VueJS is unable to identify the variable enclosed within the curly braces

I recently started learning VueJS and I've hit a roadblock. My goal is to display a variable, but for some reason, instead of seeing the product name, all I get is {{product}} Here's the HTML code I'm using: <!DOCTYPE html> <html l ...