最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to trigger method when clicking a Buefy tabs? - Stack Overflow

programmeradmin2浏览0评论

The question says it all but I have 3 tabs in Buefy, the first two (summary and details) I have got covered and work correctly as expected but the third tab is a logout button so when I click it I want to fire a method to alert("").

My buefy tabs are just from the standard page here and look like:

    <b-tabs type="is-toggle" expanded>
        <b-tab-item label="Pictures" icon="google-photos"></b-tab-item>
        <b-tab-item label="Music" icon="library-music"></b-tab-item>
        <b-tab-item label="Logout" icon="logout"></b-tab-item>
</b-tabs>

The question says it all but I have 3 tabs in Buefy, the first two (summary and details) I have got covered and work correctly as expected but the third tab is a logout button so when I click it I want to fire a method to alert("").

My buefy tabs are just from the standard page here and look like:

    <b-tabs type="is-toggle" expanded>
        <b-tab-item label="Pictures" icon="google-photos"></b-tab-item>
        <b-tab-item label="Music" icon="library-music"></b-tab-item>
        <b-tab-item label="Logout" icon="logout"></b-tab-item>
</b-tabs>

I have tried putting an on-click into the b-tab-item but that didn't work and the docs say there is an event:

input   Triggers when tab is clicked    index: Number

But I don't know how to capture that the third tab has been clicked to fire some code.

Share Improve this question asked May 29, 2019 at 22:30 Johnny John BoyJohnny John Boy 3,2946 gold badges33 silver badges58 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

As with all Vue event handlers, the @input event handler will automatically pass the event data to your methods. In this case, as the docs state, that event data is simply the index of the button tab clicked.

<template>
    <b-tabs v-model="activeTab" type="is-boxed" @input="tabClicked(activeTab)"
        <b-tab-item label="Pictures" icon="google-photos"></b-tab-item>
        <b-tab-item label="Music" icon="library-music"></b-tab-item>
        <b-tab-item label="Videos" icon="video"></b-tab-item>
    </b-tabs>
</template>

<script>
 export default {
  data() {
    return {
      activeTab: 0,
    };
  },
  methods: {
    tabClicked(index) {
      if (index === 2) alert('Index 2 is the third button');
    },
  },
};
</script>
发布评论

评论列表(0)

  1. 暂无评论