VueJS Google自动完成

时间:2018-09-12 02:16:41

标签: javascript google-maps google-maps-api-3 vue.js googleplacesautocomplete

大家好,我在实施Google自动填充时出错。 这就是场景On the blue its working fine but when I replace it to the red its not working 在蓝色上可以正常工作,但是当我将其替换为红色时则无法正常工作 result 现在,当我将其放在红色箭头指向的行上时,出现此错误 enter image description here

供参考,我使用此: https://www.npmjs.com/package/vue-google-autocomplete 我也尝试过这个 https://medium.com/dailyjs/google-places-autocomplete-in-vue-js-350aa934b18d

它工作正常,但是当我将其放在循环中时,它无法工作。它似乎 安装状态出现错误。这是我的mount()的代码

//this is the current I used
this.$refs.address.focus();
//this is for the 2nd one
/*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}`);
    });*/

1 个答案:

答案 0 :(得分:1)

用以下方法替换mounted中的行:

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

因为自动完成组件位于v-for内,所以在安装WorkHistory.vue组件时,它不知道this.$refs.address是什么,除非work_history_data至少有1个项目。