search

Home  >  Q&A  >  body text

Access plugin injection from components in Nuxt 3

<p>Using Nuxt 3 and vue-gtag, what is the correct way to access <code>$gtag</code> from a component? </p> <p>plugin/gtag.client.js:</p> <pre class="brush:php;toolbar:false;">import VueGtag from 'vue-gtag'; export default defineNuxtPlugin(nuxtApp => { const router = useRouter(); nuxtApp.vueApp.use( VueGtag, { config: { id: '...' } }, router ); });</pre> <p>In Nuxt 2, <code>this.$gtag</code> can be accessed from the component file. </p> <p>In Nuxt 3, I can't seem to find it: </p> <pre class="brush:php;toolbar:false;">const nuxtApp = useNuxtApp(); nuxtApp.$gtag //undefined</pre> <p>Looking at the source code, it seems that the definition is correct, so I don’t think it is a problem with the plug-in itself. <code>app.config.globalProperties.$gtag = api;</code></p>
P粉217629009P粉217629009501 days ago450

reply all(1)I'll reply

  • P粉709644700

    P粉7096447002023-08-25 18:39:02

    I encountered the same problem when defining the domToImage plugin in nuxt 3. I found the following solution. hope it helps you.

    • Returns what is provided in the nuxt plug-in:
    • Use in components:

    reply
    0
  • Cancelreply