向 Vue 道具添加点击事件?

时间:2021-03-09 20:13:50

标签: javascript vue.js nuxt.js

我有一个 vue 组件,它有很多道具,但遇到了一个小问题。这个 CTA 组件按钮可以有一个脚注作为道具,也可以包含一个链接,就像这个一样。即使它是一个字符串,是否可以向此链接添加点击事件(谷歌分析跟踪器)?

        <CTA
          title="Sign up for Savings"
          sub-title="Learn about the different ways<br> to save.<sup>&dagger;</sup>"
          :has-button="true"
          button-text="Sign up"
          button-href="/savings-and-support/savings"
          :has-footnote="true"
          footnote="<sup>&dagger;</sup>Restrictions apply. See <a @click='WHERE I WANT GA CLICK EVENT' href='/savings-and-support/savings#terms' style='text-decoration:underline'>eligibility requirements</a> for more information."
          @click="$ga.event('navigation', 'click', 'barker')">
        </CTA>

组件:

<template>
  <div class="cta-column" :class="{ 'cta-column--one-item': oneColumn }">
    <div class="icon">
      <slot name="icon"></slot>
    </div>

    <div class="cta callout-row__cta-title">
      <h2 v-html="title"></h2>
    </div>

    <div class="cta-sub-title">
      <p v-html="subTitle"></p>
    </div>

    <template v-if="hasButton">
      <router-link v-if="useRouterLink" :to="buttonHref" class="cta__button button" v-html="buttonText"> </router-link>

      <a v-else :href="buttonHref" class="cta__button button" v-html="buttonText"> </a>
    </template>

    <div class="cta-footnote" v-if="hasFootnote">
      <p class="footnote" v-html="footnote"></p>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CTA',
  props: {
    title: {
      type: String,
      required: true
    },
    subTitle: {
      type: String,
      required: false
    },
    hasButton: {
      type: Boolean,
      required: false
    },
    buttonText: {
      type: String,
      required: true
    },
    footnote: {
      type: String,
      required: false
    },
    hasFootnote: false,
    oneColumn: false,
    buttonHref: {
      type: String,
      required: true
    },
    useAnchor: {
      type: Boolean,
      default: false
    }
  },
}
</script>

1 个答案:

答案 0 :(得分:1)

您可以通过将 click 处理程序绑定到重新发出事件的页脚 CTA(使用 $emit() ):

click

这将允许您在需要分析的选择 p 上绑定 <p class="footnote" @click="$emit('click', $event)" v-html="footnote"></p> 处理程序(即 click)。

demo

相关问题