From 89cc96919f2ecb5dfe24099710ee67e4adf75a91 Mon Sep 17 00:00:00 2001 From: pony <1655688657@qq.com> Date: Sat, 5 Sep 2020 15:51:57 +0800 Subject: [PATCH] nextTick replace setTimeout --- lib/plugin.js | 5 +++-- lib/plugin.mock.js | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/plugin.js b/lib/plugin.js index e51ff25..7bb1faa 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -1,4 +1,5 @@ import { log } from './gtm.utils' +import Vue from 'vue' const _layer = '<%= options.layer %>' const _id = '<%= options.id %>' @@ -65,7 +66,7 @@ function gtmServer(ctx, initialized) { function startPageTracking(ctx) { ctx.app.router.afterEach((to) => { - setTimeout(() => { + Vue.nextTick().then(() => { ctx.$gtm.push(to.gtm || { routeName: to.name, pageType: 'PageView', @@ -73,7 +74,7 @@ function startPageTracking(ctx) { pageTitle: (typeof document !== 'undefined' && document.title) || '', event: '<%= options.pageViewEventName %>' }) - }, 250) + }) }) } diff --git a/lib/plugin.mock.js b/lib/plugin.mock.js index 89f67f4..c46e155 100644 --- a/lib/plugin.mock.js +++ b/lib/plugin.mock.js @@ -1,13 +1,14 @@ // This is a mock version because gtm module is disabled // You can explicitly enable module using `gtm.enabled: true` in nuxt.config import { log } from './gtm.utils' +import Vue from 'vue' const _layer = '<%= options.layer %>' const _id = '<%= options.id %>' function startPageTracking (ctx) { ctx.app.router.afterEach((to) => { - setTimeout(() => { + Vue.nextTick().then(() => { ctx.$gtm.push(to.gtm || { routeName: to.name, pageType: 'PageView', @@ -15,7 +16,7 @@ function startPageTracking (ctx) { pageTitle: (typeof document !== 'undefined' && document.title) || '', event: '<%= options.pageViewEventName %>' }) - }, 250) + }) }) }