3
votes

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>
4

4 Answers

3
votes

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://vuejs.org/v2/api/#v-on

1
votes

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/[email protected]/dist/vuetify.min.css);

#app {
  padding-top: 20px;
}
<script src="https://unpkg.com/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/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>
0
votes

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>
0
votes

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>