ElementPlus Tabs 標(biāo)簽頁

2021-09-26 17:12 更新

Tabs 標(biāo)簽頁

分隔內(nèi)容上有關(guān)聯(lián)但屬于不同類別的數(shù)據(jù)集合。

基礎(chǔ)用法

基礎(chǔ)的、簡(jiǎn)潔的標(biāo)簽頁。


用戶管理配置管理角色管理定時(shí)任務(wù)補(bǔ)償配置管理

Tabs 組件提供了選項(xiàng)卡功能,默認(rèn)選中第一個(gè)標(biāo)簽頁,你也可以通過 value 屬性來指定當(dāng)前選中的標(biāo)簽頁。

<template>
  <el-tabs v-model="activeName" @tab-click="handleClick">
    <el-tab-pane label="用戶管理" name="first">用戶管理</el-tab-pane>
    <el-tab-pane label="配置管理" name="second">配置管理</el-tab-pane>
    <el-tab-pane label="角色管理" name="third">角色管理</el-tab-pane>
    <el-tab-pane label="定時(shí)任務(wù)補(bǔ)償" name="fourth">定時(shí)任務(wù)補(bǔ)償</el-tab-pane>
  </el-tabs>
</template>

<script>
  export default {
    data() {
      return {
        activeName: 'second',
      }
    },
    methods: {
      handleClick(tab, event) {
        console.log(tab, event)
      },
    },
  }
</script>

選項(xiàng)卡樣式

選項(xiàng)卡樣式的標(biāo)簽頁。


用戶管理配置管理角色管理定時(shí)任務(wù)補(bǔ)償用戶管理

只需要設(shè)置 type 屬性為 card 就可以使選項(xiàng)卡改變?yōu)闃?biāo)簽風(fēng)格。

<template>
  <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
    <el-tab-pane label="用戶管理" name="first">用戶管理</el-tab-pane>
    <el-tab-pane label="配置管理" name="second">配置管理</el-tab-pane>
    <el-tab-pane label="角色管理" name="third">角色管理</el-tab-pane>
    <el-tab-pane label="定時(shí)任務(wù)補(bǔ)償" name="fourth">定時(shí)任務(wù)補(bǔ)償</el-tab-pane>
  </el-tabs>
</template>

<script>
  export default {
    data() {
      return {
        activeName: 'first',
      }
    },
    methods: {
      handleClick(tab, event) {
        console.log(tab, event)
      },
    },
  }
</script>

卡片化

卡片化的標(biāo)簽頁。


用戶管理配置管理角色管理定時(shí)任務(wù)補(bǔ)償用戶管理

將type設(shè)置為border-card。

<template>
  <el-tabs type="border-card">
  <el-tab-pane label="用戶管理">用戶管理</el-tab-pane>
  <el-tab-pane label="配置管理">配置管理</el-tab-pane>
  <el-tab-pane label="角色管理">角色管理</el-tab-pane>
  <el-tab-pane label="定時(shí)任務(wù)補(bǔ)償">定時(shí)任務(wù)補(bǔ)償</el-tab-pane>
</el-tabs>
</template>

位置

可以通過 tab-position 設(shè)置標(biāo)簽的位置


用戶管理配置管理角色管理定時(shí)任務(wù)補(bǔ)償用戶管理

標(biāo)簽一共有四個(gè)方向的設(shè)置 tabPosition="left|right|top|bottom"

<template>
  <el-radio-group v-model="tabPosition" style="margin-bottom: 30px;">
    <el-radio-button label="top">top</el-radio-button>
    <el-radio-button label="right">right</el-radio-button>
    <el-radio-button label="bottom">bottom</el-radio-button>
    <el-radio-button label="left">left</el-radio-button>
  </el-radio-group>

  <el-tabs :tab-position="tabPosition" style="height: 200px;">
    <el-tab-pane label="用戶管理">用戶管理</el-tab-pane>
    <el-tab-pane label="配置管理">配置管理</el-tab-pane>
    <el-tab-pane label="角色管理">角色管理</el-tab-pane>
    <el-tab-pane label="定時(shí)任務(wù)補(bǔ)償">定時(shí)任務(wù)補(bǔ)償</el-tab-pane>
  </el-tabs>
</template>

<script>
  export default {
    data() {
      return {
        tabPosition: 'left',
      }
    },
  }
</script>

自定義標(biāo)簽頁

可以通過具名 slot 來實(shí)現(xiàn)自定義標(biāo)簽頁的內(nèi)容

 

我的行程

消息中心角色管理定時(shí)任務(wù)補(bǔ)償我的行程

<template>
  <el-tabs type="border-card">
  <el-tab-pane>
    <template #label>
      <span><i class="el-icon-date"></i> 我的行程</span>
    </template>
    我的行程
  </el-tab-pane>
  <el-tab-pane label="消息中心">消息中心</el-tab-pane>
  <el-tab-pane label="角色管理">角色管理</el-tab-pane>
  <el-tab-pane label="定時(shí)任務(wù)補(bǔ)償">定時(shí)任務(wù)補(bǔ)償</el-tab-pane>
</el-tabs>
</template>

動(dòng)態(tài)增減標(biāo)簽頁增減標(biāo)簽頁按鈕只能在選項(xiàng)卡樣式的標(biāo)簽頁下使用


<template>
  <el-tabs
  v-model="editableTabsValue"
  type="card"
  editable
  @edit="handleTabsEdit"
>
  <el-tab-pane
    :key="item.name"
    v-for="(item, index) in editableTabs"
    :label="item.title"
    :name="item.name"
  >
    {{item.content}}
  </el-tab-pane>
</el-tabs>
</template>

<script>
  export default {
    data() {
      return {
        editableTabsValue: '2',
        editableTabs: [
          {
            title: 'Tab 1',
            name: '1',
            content: 'Tab 1 content',
          },
          {
            title: 'Tab 2',
            name: '2',
            content: 'Tab 2 content',
          },
        ],
        tabIndex: 2,
      }
    },
    methods: {
      handleTabsEdit(targetName, action) {
        if (action === 'add') {
          let newTabName = ++this.tabIndex + ''
          this.editableTabs.push({
            title: 'New Tab',
            name: newTabName,
            content: 'New Tab content',
          })
          this.editableTabsValue = newTabName
        }
        if (action === 'remove') {
          let tabs = this.editableTabs
          let activeName = this.editableTabsValue
          if (activeName === targetName) {
            tabs.forEach((tab, index) => {
              if (tab.name === targetName) {
                let nextTab = tabs[index + 1] || tabs[index - 1]
                if (nextTab) {
                  activeName = nextTab.name
                }
              }
            })
          }

          this.editableTabsValue = activeName
          this.editableTabs = tabs.filter((tab) => tab.name !== targetName)
        }
      },
    },
  }
</script>

自定義增加標(biāo)簽頁觸發(fā)器


<template>
  <div style="margin-bottom: 20px;">
  <el-button size="small" @click="addTab(editableTabsValue)">
    add tab
  </el-button>
</div>
<el-tabs
  v-model="editableTabsValue"
  type="card"
  closable
  @tab-remove="removeTab"
>
  <el-tab-pane
    v-for="(item, index) in editableTabs"
    :key="item.name"
    :label="item.title"
    :name="item.name"
  >
    {{item.content}}
  </el-tab-pane>
</el-tabs>
</template>

<script>
  export default {
    data() {
      return {
        editableTabsValue: '2',
        editableTabs: [
          {
            title: 'Tab 1',
            name: '1',
            content: 'Tab 1 content',
          },
          {
            title: 'Tab 2',
            name: '2',
            content: 'Tab 2 content',
          },
        ],
        tabIndex: 2,
      }
    },
    methods: {
      addTab(targetName) {
        let newTabName = ++this.tabIndex + ''
        this.editableTabs.push({
          title: 'New Tab',
          name: newTabName,
          content: 'New Tab content',
        })
        this.editableTabsValue = newTabName
      },
      removeTab(targetName) {
        let tabs = this.editableTabs
        let activeName = this.editableTabsValue
        if (activeName === targetName) {
          tabs.forEach((tab, index) => {
            if (tab.name === targetName) {
              let nextTab = tabs[index + 1] || tabs[index - 1]
              if (nextTab) {
                activeName = nextTab.name
              }
            }
          })
        }

        this.editableTabsValue = activeName
        this.editableTabs = tabs.filter((tab) => tab.name !== targetName)
      },
    },
  }
</script>

Tabs Attributes

參數(shù)說明類型可選值默認(rèn)值
model-value / v-model綁定值,選中選項(xiàng)卡的 namestring第一個(gè)選項(xiàng)卡的 name
type風(fēng)格類型stringcard/border-card
closable標(biāo)簽是否可關(guān)閉booleanfalse
addable標(biāo)簽是否可增加booleanfalse
editable標(biāo)簽是否同時(shí)可增加和關(guān)閉booleanfalse
tab-position選項(xiàng)卡所在位置stringtop/right/bottom/lefttop
stretch標(biāo)簽的寬度是否自撐開boolean-false
before-leave切換標(biāo)簽之前的鉤子,若返回 false 或者返回 Promise 且被 reject,則阻止切換。Function(activeName, oldActiveName)

Tabs Events

事件名稱說明回調(diào)參數(shù)
tab-clicktab 被選中時(shí)觸發(fā)被選中的標(biāo)簽 tab 實(shí)例
tab-remove點(diǎn)擊 tab 移除按鈕后觸發(fā)被刪除的標(biāo)簽的 name
tab-add點(diǎn)擊 tabs 的新增按鈕后觸發(fā)
edit點(diǎn)擊 tabs 的新增按鈕或 tab 被關(guān)閉后觸發(fā)(targetName, action)

Tab-pane Attributes

參數(shù)說明類型可選值默認(rèn)值
label選項(xiàng)卡標(biāo)題string
disabled是否禁用booleanfalse
name與選項(xiàng)卡綁定值 value 對(duì)應(yīng)的標(biāo)識(shí)符,表示選項(xiàng)卡別名string該選項(xiàng)卡在選項(xiàng)卡列表中的順序值,如第一個(gè)選項(xiàng)卡則為'1'
closable標(biāo)簽是否可關(guān)閉booleanfalse
lazy標(biāo)簽是否延遲渲染booleanfalse

Tab-pane Slots

name說明
-Tab-pane 的內(nèi)容
labelTab-pane 的標(biāo)題內(nèi)容


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)