nuxt-google-maps-module icon indicating copy to clipboard operation
nuxt-google-maps-module copied to clipboard

Following the short example runs code before script is loaded.

Open robbylucia opened this issue 5 years ago • 4 comments

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?

robbylucia avatar Jan 18 '20 23:01 robbylucia

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();
    });
  },

harlet avatar Mar 31 '20 23:03 harlet

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

pbjer avatar Apr 23 '20 19:04 pbjer

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

harlet avatar Apr 23 '20 19:04 harlet

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

pbjer avatar Apr 23 '20 21:04 pbjer