olefirenko / vue-google-autocomplete

A Vue.js autosuggest component for the Google Places API.
https://olefirenko.github.io/vue-google-autocomplete/
MIT License
507 stars 258 forks source link

How can I set a default address when page loads? #136

Open debanjanpatra opened 4 years ago

antoineguillaume commented 4 years ago

Here is how I did it. I added the property "value" to the component and I linked it to a computed property in my view.

<vue-google-autocomplete
  :types="getType"
  :value="setAddressData"
  @placechanged="getAddressData
>
</vue-google-autocomplete>
computed: {
  getType () {
    return '(cities)'
  },
  setAddressData () {
    return this.getType === '(cities)' ? `${this.address.city}, ${this.address.country}` : `${this.address.line1}, ${this.address.city}, ${this.address.country}`
  }
}

Here getType computed property is fixed for the example but in my case it's dynamic. Then, the way we want to display the address in the autocomplete field depends on that. And this.address object is your default address.

Hope this helps !

ghost commented 2 years ago

@antoineguillaume I am trying to accomplish something similar (a user can update their existing address), but the computed solution you posted isn't working, and I think it's because my autocomplete component doesn't show to the user unless they click a button, and then it expands using v-if, do you have any ideas on how to get the component to have a default value (just a string of text) without using the update() method in mounted() or without using the computed solution you posted above?