Access plugin injection from components in Nuxt 3
P粉217629009
P粉217629009 2023-08-24 23:26:49
0
1
415
<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粉217629009
P粉217629009

reply all(1)
P粉709644700

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:
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template