Nuxt的google-gtag中的Google标签(gtag)无法在组件中识别

问题描述 投票:0回答:1

我正在从https://github.com/nuxt-community/google-gtag将gtag用于nuxt。我已经正确地将它包含在nuxt.config.js的modules中,并且文档说:

此模块在您的NuxtJs项目中包含Google gtag,默认情况下启用每个页面跟踪。您可以在组件/函数/方法内部使用gtag,如下所示:

this.$gtag('event', 'your_event', { /* track something awesome */})

我正在跟踪我的下载按钮:

<button class="download_button" @click="
                  this.$gtag('event', 'DownloadButton', {
                    event_category: 'Article',
                    event_label: 'Product name',
                  })
                "
              > 
                Download Now
</button>

单击按钮时收到此错误:

TypeError: Cannot read property '$gtag' of null at clickProperty '$gtag' does not exist on type 'ArticleContent',当我在脚本标签中使用它时。

导入该模块时我丢失了什么东西吗?我真的很感谢您的帮助。

javascript vue.js nuxt.js nuxt gtag.js
1个回答
0
投票

如果在模板中使用this,请尝试将其删除:

<button 
  class="download_button" @click="$gtag('event', 'DownloadButton', {
      event_category: 'Article',
      event_label: 'Product name',
    })
  "
> 
  Download Now
</button>
© www.soinside.com 2019 - 2024. All rights reserved.