WilliamDASILVA / nuxt-google-maps-module

Inject Google Maps script
MIT License
22 stars 8 forks source link

Following the short example runs code before script is loaded. #13

Open robbylucia opened 4 years ago

robbylucia commented 4 years ago

Am I doing something wrong? I followed the short example, but when I have something on mounted(), it says google is undefined. When I put it on a function I can execute (seconds after the page loads), it works fine. Considering how I followed the short example exactly, I'm assuming something changed, either with Nuxt or with Google Maps API. Can anyone replicate, and is there a fix?

harlet commented 4 years ago

Using the same trick he did in the plugin: https://github.com/WilliamDASILVA/nuxt-google-maps-module/blob/master/src/plugin.template.js#L18

  mounted() {
    const that = this;
    const event = new window.Event('maps-module:initiated');
    window.dispatchEvent(event);
    window.addEventListener('maps-module:loaded', () => {
      that.initGoogleApi();
    });
  },
pbjer commented 4 years ago

@harlet following your example results in that.initGoogleApi is not a function

harlet commented 4 years ago

Because that is a component method. :) Inside the event function, you can use this.$google as you would normally do.

pbjer commented 4 years ago

Ok thank you for the clarification. I thought you were implying initGoogleApi was bound to the this context already. :)