Autocomplete feature for Google Places using VueJS

Hey everyone, I'm encountering an issue while trying to implement the Google Autocomplete feature. Here's what's happening https://i.sstatic.net/CZsED.png It works fine with the blue version, but when I switch to the red one, it stops working https://i.sstatic.net/e3KrE.png Now, when I place it on the line indicated by the red arrow, I get this error https://i.sstatic.net/jIsB0.png

For guidance, I am using this package: https://www.npmjs.com/package/vue-google-autocomplete I also tested out this resource https://medium.com/dailyjs/google-places-autocomplete-in-vue-js-350aa934b18d

Everything functions correctly until I try implementing it within a loop. It appears that the mounted state is causing an error. Below is the code for my mounted() method:

//this is the current approach I used
this.$refs.address.focus();
//this is for the second option
/*this.autocomplete = new google.maps.places.Autocomplete(
      (this.$refs.autocomplete),
      {types: ['geocode']}
    );
    this.autocomplete.addListener('place_changed', () => {
      let place = this.autocomplete.getPlace();
      let ac = place.address_components;
      let lat = place.geometry.location.lat();
      let lon = place.geometry.location.lng();
      let city = ac[0]["short_name"];

      console.log(`The user picked ${city} with the coordinates ${lat}, ${lon}`);
    });*/

Answer №1

Make sure to update the line in your mounted method with the following:

if(this.$refs.address) this.$refs.address.focus();

This is necessary because the autocomplete component is nested within a v-for loop. As a result, when the WorkHistory.vue component is initialized, it may not recognize the reference to this.$refs.address unless there is at least one item in the work_history_data.

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 spacious text box for an enhanced Ajax search feature

I'm currently working on an AJAX application that allows users to input the name of a movie, and then loads results from the database through jquery using a PHP API. However, I'm facing a challenge in implementing a text box with the following re ...

Troublesome Zopim Widget Design Dilemma

After adding the Zopim widget code to the head section of my website, I found that it consistently loads with a black color theme, ignoring any color settings I try to apply. I've attempted to adjust the styles using CSS and Javascript, but so far not ...

What is the best way to sum two values in an array object and then replace the first value with the result?

I have an array containing two objects: let xyz = [ {amount: 1000, rate: 4}, {amount: 100, rate: 2}, {amount: 700, rate: 1} ]; The task is to multiply amount * rate and update the values in the amount key. Desired output: let result = [ {amo ...

minimize the size of the image within a popup window

I encountered an issue with the react-popup component I am using. When I add an image to the popup, it stretches to full width and length. How can I resize the image? export default () => ( <Popup trigger={<Button className="button" ...

Issue found with Qualtrics survey: A blank button added via jquery causes page to refresh

I'm facing a challenge while trying to carry out an experiment in Qualtrics. Whenever I add a button to a Qualtrics question using JQuery, the page gets refreshed automatically even if I haven't defined .click or .event. This results in erasing ...

Close the menu in Angular 7 by clicking outside of it

I am seeking a way to detect when a click event occurs inside the parent div #menu, regardless of any HTML tags present within it. Using nativeElement.parent did not provide the desired result. Here is the HTML code: <button #toggleButton (click)="to ...

React is unable to identify the `activeKey` property on a DOM element

First and foremost, I am aware that there have been a few inquiries regarding this particular error, although stemming from differing sources. Below is the snippet of my code: <BrowserRouter> <React.Fragment> <Navbar className=& ...

Ensuring all fetch requests are completed successfully before dispatching an action in React-Redux

I'm currently developing a React application and utilizing Redux to manage the state. Here is a snippet of the code: category-arrows.component.jsx: import React, { Component } from 'react'; import { connect } from 'react-redux'; i ...

The console is showing an 'undefined' return value for the $index variable in Angular

Hi there, I've encountered a problem with some code I wrote, and I'm hoping someone can help me figure out what's going on. This particular snippet of code is supposed to run when the menu buttons are clicked. $scope.show = function($inde ...

What is the data type of the $result.rows.item(x) variable in Javascript?

I have a function that reads and displays the contents of a SELECT query from WEBSQL in HTML5. I need to reuse this function, but I am facing an issue because it returns an Array of JSON objects and I want to convert it to rows.item(). Does anyone know how ...

Step-by-Step Guide on Dividing Table Row Data into Two Distinct Tables

At present, I have created a dynamic table that retrieves data from the database using forms in Django. I'm facing an issue with this table as even though there are 7 columns, only 2 of them are visible. However, all 5 hidden columns do contain impor ...

Guide on displaying the contents of a log file in a new window upon clicking a link within a Spring MVC application

One of my main objectives is to have a new browser window open and display the content of an entire log file when a link is clicked. This window should not show an address bar or navigation buttons (Back, Forward). I am currently working on this task in a ...

Hide the scrollbar on an iframe

I have been trying to modify the attributes of an iframe using javascript in the following way: var iFrame = window.top.document.getElementById('window_content'); iFrame.setAttribute("height","63"); iFrame.setAttribute("scrolling","no") ...

Angular UI Bootstrap modal experiencing issues with model binding

Can someone help me with a simple example using Angular UI Bootstrap modal service? I am having trouble with model binding as the expected "Doing something..." message is not displaying on the modal dialog, instead "{{message}}" is shown. What changes do I ...

Exploring ways to dynamically alter templates using the link function in Angular.js

Recently, I developed a directive called widget which functions similar to ng-view, but the template name is derived from an attribute. app.directive('widget', function() { return { restrict: 'EA', scope: true, ...

"Exploring the implementation of 'var' in JavaScript for naming conventions

I'm looking to incorporate a random number into the window name in this code snippet: var rand = 185656; setTimeout("win+rand+2 = window.open('url'+secid , '_blank')",2000); The +rand+ part is not functioning as expected. ...

Developing a personalized data binding feature with AngularJS and utilizing ng-repeat

Looking to design a unique controller that can display multiple similar objects connected to a dataset structured like this: [{name: card1, values: {opt1: 9, opt2: 10}},{name: card1, values: {opt1: 9, opt2: 10}}] The goal is to showcase the name and one ...

Configuring Google Maps API (including charts) for maximum height of 100%

I am having trouble getting my map to display at 100% height using the Google Maps API. I've encountered similar issues in the past with the Google Charts API as well. From what I've gathered, it seems like setting the height of the html and bod ...

What is the best way to access ASP.NET Controls using Javascript?

Currently, I am developing my very first ASP.NET application. I have successfully created text boxes and buttons, and implemented client-side validation for the text boxes. Now, I want to dynamically set and clear the enabled property of the buttons based ...

Automatic cancellation of AJAX request

Having a strange issue that I need help with. I am using a standard ajax call to upload avatars from the user's PC to the server. However, sometimes I notice in Firebug that the request is being "Aborted" and marked in red after loading for some time ...