Skip to content

Commit

Permalink
refactor: use composable useRouter to get nuxt router
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisMazel committed May 22, 2023
1 parent bdc6f33 commit 3624f1c
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { defineNuxtPlugin } from 'nuxt/app'

import { defineNuxtPlugin, useRouter } from 'nuxt/app'
import { createGtm, type VueGtmUseOptions } from '@gtm-support/vue-gtm'
import type { ModuleOptions } from '../module'
// @ts-ignore
import gtmOptions from '#gtm'

export default defineNuxtPlugin(({ vueApp }) => {
export default defineNuxtPlugin((nuxt) => {
if (process.client) {
const options = gtmOptions as ModuleOptions
const options = nuxt.$config.public.gtm

const vueRouter = vueApp.$nuxt?.$router
const router = useRouter()

const pluginOptions: VueGtmUseOptions = {
...options,
vueRouter: options.enableRouterSync && vueRouter ? vueRouter : undefined
vueRouter: options.enableRouterSync && router ? router as VueGtmUseOptions['vueRouter'] : undefined
}

vueApp.use(createGtm(pluginOptions))
nuxt.vueApp.use(createGtm(pluginOptions))
}
})

0 comments on commit 3624f1c

Please sign in to comment.