Using Vue.js along with TypeScript and vue-property-decorator for enabling Google Place Autocomplete functionality

<template>
  <b-form-textarea
    :id="id"
    ref="autocomplete"
    v-model="autocompleteText"
    rows="3"
    max-rows="6"
    type="text"
    :class="classname"
    :placeholder="placeholder"
    @focus="onFocus()"
    @blur="onBlur()"
    @change="onChange"
    @keypress="onKeyPress"
    @keyup="onKeyUp"
  />
</template>
<script lang="ts">
import { Component, Prop, Vue, Watch } from 'vue-property-decorator';

    @Component({
      name: 'AddressInput'
    })
export default class extends Vue {
  @Watch('autocompleteText')
  private autocompleteTextChange(newVal:any, oldVal:any) {
    this.$emit('inputChange', { newVal, oldVal }, this.id);
  }

  @Watch('country')
  private countryChange(newVal:any, oldVal:any) {
    this.autocomplete.setComponentRestrictions({
      country: this.country === null ? [] : this.country
    });
  }

  mounted() {
    const options:any = {};
    if (this.types) {
      options.types = [this.types];
    }

    if (this.country) {
      options.componentRestrictions = {
        country: this.country
      };
    }

    this.autocomplete = new window.google.maps.places.Autocomplete(
        document.getElementById(this.id),
        options
    );

    this.autocomplete.addListener('place_changed', this.onPlaceChanged);
  }

  private onPlaceChanged() {
    let place = this.autocomplete.getPlace();

    if (!place.geometry) {
      this.$emit('no-results-found', place, this.id);
      return;
    }

    if (place.address_components !== undefined) {
      this.$emit('placechanged', this.formatResult(place), place, this.id);

      this.autocompleteText = (document.getElementById(this.id) as HTMLInputElement).value;
      this.onChange()
    }
  }

}
</script>

I am currently working on a Google Place Autocomplete component using Vue.js, TypeScript, and vue-property-decorator. The autocomplete place suggestions are appearing correctly. However, I am encountering an error message "InvalidValueError: not an instance of HTMLInputElement". I have attempted using $refs but the same error persists.

If you have any questions or need clarification, feel free to ask.

Any insights on what might be causing this issue would be greatly appreciated. Thank you in advance.

https://i.sstatic.net/YYQoG.png

https://i.sstatic.net/yOZws.png

Answer №1

The error message reads "not a valid HTMLInputElement instance."

Try switching from using the textarea element to an input element.

<input type="text />

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

Creating a connection object for MySql2 in TypeScript within a class

I'm still a beginner when it comes to TypeScript, so I often stumble over simple things. Initially, I had no issues creating a database connection using the code snippet below that was not placed inside a class: const mysql = require('mysql2&apos ...

Unable to display values in Fusion Charts zoomline chart using the showValues chart property

I'm struggling to figure out how to display the data plot values with showValues: '1' in a zoomline chart using Fusion Charts. You can see and test it on this fiddle: http://jsfiddle.net/60oeahc1/4/ Is there a way to make this feature work ...

Experiencing a Typescript issue while trying to set a string as the state of a React component with a specified TS type

I've defined a state in my React component for a specific data type called Color. \\ state const [messageSeverity, setMessageSeverity] = useState<Color>('success'); \\ TS type export type Color = 'success&ap ...

Exploring the implementation of a custom validator within an Angular service

I have been attempting to implement a custom validator to validate if an email is already in use. After consulting the documentation and reading various articles, I have come up with the following code: In my auth.service.ts file checkEmail(email) { ...

Obtaining the base name of all subpaths within a given string

I'm fairly new to working with Vue and JS. Let's consider an array of objects where each object contains a "name" field, representing various processes or commands. For instance: /disks/tools/itools/odor/src/bin/lib/script.py -path /disks/tools/ ...

Using Angular, a function can be called recursively directly from within the HTML

I'm struggling with loading an image because the method getUserProfileImage() is getting triggered multiple times within a loop. Is there a way to ensure the method is only called once during each iteration? I understand that this issue is related to ...

Retrieve the attribute from the element that is in the active state

I'm facing a challenge in determining the active status of an element attribute. I attempted the following approach, but it incorrectly returned false even though the element had the attribute in an active state - (.c-banner.active is present) During ...

Increasing an ID number automatically using Javascript

I'm currently working on a functionality where a unique number is automatically generated whenever a new record is created. For instance, if I were to click "Create record" on a webpage, the number would auto-fill in the record ID field. Subsequently, ...

Personalize the position of the v-select drop-down menu

I am currently working with the vuetify v-select component. The problem I am encountering is that instead of the dropdown opening downwards, I need it to open upwards since the dropdown is positioned at the bottom of the page which causes some of the dro ...

Encountering a problem while attempting to host an Angular application on localhost:4200

After executing the ng serve command, I encountered an issue in the browser: An error occurred while trying to resolve "localhost:4200" ("") for "10.238.0.0": rpc error: code = Unknown desc = no such record I apologize if this question seems basic, as I ...

The BehaviorSubject will consistently emit identical values to each subscription

I am currently facing an issue with the BehaviorSubject where it emits a value for every subscription. This means that if I have, for example, 2 subscriptions to this.projectService.projectState$ streams using methods like async or tap, the projectState$ e ...

unable to form the directory named tns_modules

Facing an issue while trying to run the demo application in Nativescript. Performed the following command: tns run ios Encountered an error: Unable to apply changes on device: DEVICE_ID. Error is: Processing node_modules failed. Error: cp: cannot c ...

Mastering the Use of *ngIf with *ngFor: Best Practices for Proper Implementation

Can someone help me rewrite the combination of *ngIF and *ngFor below? I understand that my issue may be similar to others, but please know that this is different. Everything seems to be working fine. The only problem I'm facing is that the color of ...

What could be causing the Vue data to fail to update in this particular scenario?

I'm having trouble grasping Vue completely. For example, consider the following code snippets: <!--store.js--> export default { x:{ p:{}, }, } <!--text.vue--> <template> <div class="test-comp">Test componen ...

Nuxt 3: Accessing the contents of a slot

Here is the code snippet for my component. I am incorporating VueUse as well, and I aim to activate the "onClickOutside" function of vueUse when a click occurs outside the slot element. However, I have been facing challenges in identifying the c ...

Angular XOR operation between two strings

I need to perform XOR operation on two strings and I found a python implementation for it: def sxor(s1,s2): return ''.join(chr(ord(a) ^ ord(b)) for a,b in zip(s1,s2)) In the code above, a for loop is used to iterate over the strings and ...

Hold off until the observable has finished

map((tasks): any => { return tasks.map(task => ({ ...task, status: this.getStatus(task.owner, task.delegationState, task.assignee, task.id), })); }); I utilize the getStatus method within the map() operator from rxjs. getStatus( ow ...

Is there a way to ensure an ajax call finishes executing without relying on 'async: false' or callbacks?

In my view, I have implemented a TypeScript code defining a KnockoutJS binding handler for a clickable element as shown below: module MyModule { export interface ICopyButtonParams { dataUrl: string; } ko.bindingHandlers.copyButton = { ...

Choosing a single element through viewChild using the "#" selector in Angular 2

Is there a special method to choose multiple tags on the same level with the same tag? <div #el></div> <div #el></div> <div #el></div> I keep getting an error message that says "Reference "#el" is defined several times ...

Is it possible for pdfjs-dist to be used with Typescript?

Is there a way to preview a PDF as a canvas without importing pdfjs-dist into my project? I have already used the command $yarn add pdfjs-dist to install pdfjs-dist. Do I need to include any additional imports? import pdfjsLib from "pdfjs-dist/build ...