VueJS Google 地方自动完成

VueJS Google places autocomplete

大家好,我在实现 google 自动完成时遇到了错误。 所以这是场景 在蓝色上它工作正常但是当我将它替换为红色时它不工作 现在当我把它放在红色箭头指向的线上时,我得到了这个错误

作为参考我正在使用这个: https://www.npmjs.com/package/vue-google-autocomplete 我也试过这个 https://medium.com/dailyjs/google-places-autocomplete-in-vue-js-350aa934b18d

它工作正常,但是当我把它放在循环中时它不工作。它似乎 安装状态出错。这是我的代码 mounted()

//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}`);
    });*/

mounted 中的行替换为:

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

因为自动完成组件放在v-for里面所以当WorkHistory.vue组件被挂载时,它不知道this.$refs.address是什么,除非work_history_data有至少 1 项。