Search code examples
vue.jsvuejs2tabsvue-componentelement-ui

How to go to specific tab in el-tabs when the button is clicked


I have tabs using element ui:

<template>
  <div>
    <el-tabs v-model="activeName" @tab-click="handleClick">
    <el-tab-pane label="User" name="first">User</el-tab-pane>
    <el-tab-pane label="Config" name="second">Config</el-tab-pane>
    <el-tab-pane label="Role" name="third">Role</el-tab-pane>
    <el-tab-pane label="Task" name="fourth">Task</el-tab-pane>
  </el-tabs>
  <button @click="handleTab"></button>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        activeName: 'first'
      };
    },
    methods: {
      handleClick(tab, event) {
        console.log(tab, event);
      }
    }
  };
</script>

And when I click the button handleTab I want to go to the first tab which is User in this example. Is there any way to do that?


Solution

  • You should create method handleTab which you already call on button click and in that method set value of activeName to first.

    <template>
      <div>
        <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="User" name="first">User</el-tab-pane>
        <el-tab-pane label="Config" name="second">Config</el-tab-pane>
        <el-tab-pane label="Role" name="third">Role</el-tab-pane>
        <el-tab-pane label="Task" name="fourth">Task</el-tab-pane>
      </el-tabs>
      <button @click="handleTab">Open User</button>
      </div>
    </template>
    <script>
      export default {
        data() {
          return {
            activeName: 'first'
          };
        },
        methods: {
          handleClick(tab, event) {
            console.log(tab, event);
          },
          handleTab() {
            this.activeName = 'first';
          }
        }
      };
    </script>