3

We recently started using Vue and Vuetify. As part of the application, I need to write click action on Vuetify badge, but not sure why it's not working. I tried the following code snippet:

<v-badge bottom
               left
               overlap
               :color="red">
        <div slot="badge"
             @click="togglePopover"
             class="availability"></div>
        <Avatar :objData="data.image"
                :size="size"
                :applyBoarder="applyBoarder">
          <slot></slot>
        </Avatar>
      </v-badge>

<script>
  export default {
    methods: {
    togglePopover(e) {
      alert('click action');
    }
  }
</script>
tony19
  • 125,647
  • 18
  • 229
  • 307
subbu
  • 562
  • 3
  • 8
  • 21

4 Answers4

3

You should use the native modifier on you click event.

@click.native="togglePopover"

From the VueJS documentation

.native - listen for a native event on the root element of component.

For more information and all the available modifiers click here: https://v2.vuejs.org/v2/api/#v-on

tony19
  • 125,647
  • 18
  • 229
  • 307
Tarabass
  • 3,132
  • 2
  • 17
  • 35
1

Instead of a div for the badge slot, you can use a v-btn:

<v-badge>
  <v-btn slot="badge" @click="togglePopover">
    <v-icon>done</v-icon>
  </v-btn>
</v-badge>

new Vue({
  el: '#app',
  methods: {
    togglePopover() {
      console.log('click');
    }
  }
})
@import url(https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons);
@import url(https://cdn.jsdelivr.net/npm/vuetify@1.2.6/dist/vuetify.min.css);

#app {
  padding-top: 20px;
}
<script src="https://unpkg.com/vue@2.5.17"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@1.2.6/dist/vuetify.min.js"></script>

<div id="app">
  <v-app id="inspire">
    <div class="text-xs-center">
      <v-badge
        color="purple"
        left
        overlap
      >
        <v-btn slot="badge"
               flat
               icon
               dark
               small
               :ripple="false"
               @click="togglePopover">
          <v-icon>done</v-icon>
        </v-btn>
        <v-icon
          color="grey lighten-1"
          large
        >
          account_circle
        </v-icon>
      </v-badge>
    </div>
  </v-app>
</div>
tony19
  • 125,647
  • 18
  • 229
  • 307
1

For me, @tony19's answer did not work unless adding a z-index

<v-badge
    bordered
    color="red"
    overlap
>
    <v-icon slot="badge" style="z-index: 1;" @click="doSomething">
        mdi-close
    </v-icon>
    <v-img
        class="rounded-lg"
        height="100"
        width="100"
        aspect-ratio="1"
        src="https://placekitten.com/g/300/300"
    />
</v-badge>
Sølve T.
  • 4,159
  • 1
  • 20
  • 31
0

I found that the item the badge was attached to works when clicked, but not the badge itself. Here is my workaround: Add a div to display content slot, in addition to the item wrapped in the badge.

This example uses data value count as the content of the badge.

      <v-badge>
        <!-- WORKAROUND badge is not clickable -->
        <template #badge>
          <div
              style="cursor: pointer"
              @click="$emit('show-settings')">
            {{ String(count) }}
          </div>
        </template>
        <v-btn icon @click="$emit('show-settings')">
          <v-icon>settings</v-icon>
        </v-btn>
      </v-badge>
Steven Spungin
  • 27,002
  • 5
  • 88
  • 78