2016-08-26 18:34:54 +08:00
|
|
|
|
## Tabs 标签页
|
2016-12-22 00:02:51 +08:00
|
|
|
|
|
2016-09-07 18:36:50 +08:00
|
|
|
|
分隔内容上有关联但属于不同类别的数据集合。
|
2016-07-27 14:15:02 +08:00
|
|
|
|
|
2016-09-06 12:47:25 +08:00
|
|
|
|
### 基础用法
|
|
|
|
|
|
|
|
|
|
基础的、简洁的标签页。
|
2016-07-27 14:15:02 +08:00
|
|
|
|
|
2016-12-22 00:02:51 +08:00
|
|
|
|
:::demo Tabs 组件提供了选项卡功能,默认选中第一个标签页,你也可以通过 `value` 属性来指定当前选中的标签页。
|
2016-07-27 14:15:02 +08:00
|
|
|
|
|
|
|
|
|
```html
|
2016-08-22 17:18:00 +08:00
|
|
|
|
<template>
|
2016-12-22 00:02:51 +08:00
|
|
|
|
<el-tabs v-model="activeName" @tab-click="handleClick">
|
2016-11-16 16:14:48 +08:00
|
|
|
|
<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="定时任务补偿" name="fourth">定时任务补偿</el-tab-pane>
|
2016-08-22 17:18:00 +08:00
|
|
|
|
</el-tabs>
|
|
|
|
|
</template>
|
|
|
|
|
<script>
|
|
|
|
|
export default {
|
|
|
|
|
data() {
|
|
|
|
|
return {
|
2017-01-04 12:32:00 +08:00
|
|
|
|
activeName: 'second'
|
2016-08-22 17:18:00 +08:00
|
|
|
|
};
|
2016-12-22 00:02:51 +08:00
|
|
|
|
},
|
|
|
|
|
methods: {
|
|
|
|
|
handleClick(tab, event) {
|
|
|
|
|
console.log(tab, event);
|
|
|
|
|
}
|
2016-08-22 17:18:00 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
</script>
|
2016-07-27 14:15:02 +08:00
|
|
|
|
```
|
2016-08-25 18:24:51 +08:00
|
|
|
|
:::
|
2016-07-27 14:15:02 +08:00
|
|
|
|
|
2016-09-06 12:47:25 +08:00
|
|
|
|
### 选项卡样式
|
2016-08-22 17:18:00 +08:00
|
|
|
|
|
2016-09-06 12:47:25 +08:00
|
|
|
|
选项卡样式的标签页。
|
2016-07-27 14:15:02 +08:00
|
|
|
|
|
2016-12-22 00:02:51 +08:00
|
|
|
|
:::demo 只需要设置 `type` 属性为 `card` 就可以使选项卡改变为标签风格。
|
2016-07-27 14:15:02 +08:00
|
|
|
|
|
2016-09-06 12:47:25 +08:00
|
|
|
|
```html
|
|
|
|
|
<template>
|
2016-12-22 00:02:51 +08:00
|
|
|
|
<el-tabs v-model="activeName2" 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="定时任务补偿" name="fourth">定时任务补偿</el-tab-pane>
|
2016-09-06 12:47:25 +08:00
|
|
|
|
</el-tabs>
|
|
|
|
|
</template>
|
|
|
|
|
<script>
|
|
|
|
|
export default {
|
2016-12-22 00:02:51 +08:00
|
|
|
|
data() {
|
|
|
|
|
return {
|
2017-02-27 13:33:14 +08:00
|
|
|
|
activeName2: 'first'
|
2016-12-22 00:02:51 +08:00
|
|
|
|
};
|
|
|
|
|
},
|
2016-09-06 12:47:25 +08:00
|
|
|
|
methods: {
|
2016-11-13 11:58:45 +08:00
|
|
|
|
handleClick(tab, event) {
|
|
|
|
|
console.log(tab, event);
|
2016-09-06 12:47:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
</script>
|
2016-07-27 14:15:02 +08:00
|
|
|
|
```
|
2016-08-25 18:24:51 +08:00
|
|
|
|
:::
|
2016-07-27 14:15:02 +08:00
|
|
|
|
|
2016-09-06 12:47:25 +08:00
|
|
|
|
### 卡片化
|
|
|
|
|
|
|
|
|
|
卡片化的标签页。
|
|
|
|
|
|
|
|
|
|
:::demo 将`type`设置为`border-card`。
|
|
|
|
|
```html
|
|
|
|
|
<el-tabs type="border-card">
|
2016-11-16 16:14:48 +08:00
|
|
|
|
<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="定时任务补偿">定时任务补偿</el-tab-pane>
|
2016-09-06 12:47:25 +08:00
|
|
|
|
</el-tabs>
|
2016-07-27 14:15:02 +08:00
|
|
|
|
```
|
2016-08-25 18:24:51 +08:00
|
|
|
|
:::
|
2016-07-27 14:15:02 +08:00
|
|
|
|
|
2017-07-30 16:12:06 +08:00
|
|
|
|
### 位置
|
|
|
|
|
|
|
|
|
|
可以通过 `tab-position` 设置标签的位置
|
|
|
|
|
|
|
|
|
|
:::demo 标签一共有四个方向的设置 `tabPosition="left|right|top|bottom"`
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
<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="定时任务补偿">定时任务补偿</el-tab-pane>
|
|
|
|
|
</el-tabs>
|
|
|
|
|
</template>
|
|
|
|
|
<script>
|
|
|
|
|
export default {
|
|
|
|
|
data() {
|
|
|
|
|
return {
|
|
|
|
|
tabPosition: 'top'
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
</script>
|
|
|
|
|
```
|
|
|
|
|
:::
|
|
|
|
|
|
2016-12-22 14:03:03 +08:00
|
|
|
|
### 自定义标签页
|
|
|
|
|
|
2016-12-29 16:19:12 +08:00
|
|
|
|
可以通过具名 `slot` 来实现自定义标签页的内容
|
2016-12-22 14:03:03 +08:00
|
|
|
|
|
2016-12-29 16:19:12 +08:00
|
|
|
|
:::demo
|
2016-12-22 14:03:03 +08:00
|
|
|
|
```html
|
|
|
|
|
<el-tabs type="border-card">
|
2016-12-29 16:19:12 +08:00
|
|
|
|
<el-tab-pane>
|
|
|
|
|
<span slot="label"><i class="el-icon-date"></i> 我的行程</span>
|
|
|
|
|
我的行程
|
|
|
|
|
</el-tab-pane>
|
2016-12-22 14:03:03 +08:00
|
|
|
|
<el-tab-pane label="消息中心">消息中心</el-tab-pane>
|
|
|
|
|
<el-tab-pane label="角色管理">角色管理</el-tab-pane>
|
|
|
|
|
<el-tab-pane label="定时任务补偿">定时任务补偿</el-tab-pane>
|
|
|
|
|
</el-tabs>
|
|
|
|
|
```
|
|
|
|
|
:::
|
|
|
|
|
|
2017-01-12 22:46:26 +08:00
|
|
|
|
### 动态增减标签页
|
|
|
|
|
|
|
|
|
|
增减标签页按钮只能在选项卡样式的标签页下使用
|
|
|
|
|
|
|
|
|
|
:::demo
|
|
|
|
|
```html
|
|
|
|
|
<el-tabs v-model="editableTabsValue" type="card" editable @edit="handleTabsEdit">
|
|
|
|
|
<el-tab-pane
|
2017-04-28 14:03:42 +08:00
|
|
|
|
:key="item.name"
|
2017-01-12 22:46:26 +08:00
|
|
|
|
v-for="(item, index) in editableTabs"
|
|
|
|
|
:label="item.title"
|
|
|
|
|
:name="item.name"
|
|
|
|
|
>
|
|
|
|
|
{{item.content}}
|
|
|
|
|
</el-tab-pane>
|
|
|
|
|
</el-tabs>
|
|
|
|
|
<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>
|
|
|
|
|
```
|
|
|
|
|
:::
|
2016-12-22 00:02:51 +08:00
|
|
|
|
|
2017-01-12 22:46:26 +08:00
|
|
|
|
### 自定义增加标签页触发器
|
2016-12-22 00:02:51 +08:00
|
|
|
|
|
|
|
|
|
:::demo
|
|
|
|
|
```html
|
|
|
|
|
<div style="margin-bottom: 20px;">
|
2017-01-12 22:46:26 +08:00
|
|
|
|
<el-button
|
|
|
|
|
size="small"
|
|
|
|
|
@click="addTab(editableTabsValue2)"
|
|
|
|
|
>
|
|
|
|
|
add tab
|
|
|
|
|
</el-button>
|
2016-12-22 00:02:51 +08:00
|
|
|
|
</div>
|
2017-01-12 22:46:26 +08:00
|
|
|
|
<el-tabs v-model="editableTabsValue2" type="card" closable @tab-remove="removeTab">
|
|
|
|
|
<el-tab-pane
|
|
|
|
|
v-for="(item, index) in editableTabs2"
|
2017-04-28 14:03:42 +08:00
|
|
|
|
:key="item.name"
|
2017-01-12 22:46:26 +08:00
|
|
|
|
:label="item.title"
|
|
|
|
|
:name="item.name"
|
|
|
|
|
>
|
|
|
|
|
{{item.content}}
|
|
|
|
|
</el-tab-pane>
|
2016-12-22 00:02:51 +08:00
|
|
|
|
</el-tabs>
|
2017-01-12 22:46:26 +08:00
|
|
|
|
<script>
|
|
|
|
|
export default {
|
|
|
|
|
data() {
|
|
|
|
|
return {
|
|
|
|
|
editableTabsValue2: '2',
|
|
|
|
|
editableTabs2: [{
|
|
|
|
|
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.editableTabs2.push({
|
|
|
|
|
title: 'New Tab',
|
|
|
|
|
name: newTabName,
|
|
|
|
|
content: 'New Tab content'
|
|
|
|
|
});
|
|
|
|
|
this.editableTabsValue2 = newTabName;
|
|
|
|
|
},
|
|
|
|
|
removeTab(targetName) {
|
|
|
|
|
let tabs = this.editableTabs2;
|
|
|
|
|
let activeName = this.editableTabsValue2;
|
|
|
|
|
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.editableTabsValue2 = activeName;
|
|
|
|
|
this.editableTabs2 = tabs.filter(tab => tab.name !== targetName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
2016-12-22 00:02:51 +08:00
|
|
|
|
```
|
|
|
|
|
:::
|
|
|
|
|
|
2016-08-26 18:34:54 +08:00
|
|
|
|
### Tabs Attributes
|
2016-07-27 14:15:02 +08:00
|
|
|
|
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|
|
|
|
|
|---------- |-------- |---------- |------------- |-------- |
|
2019-01-23 11:34:19 +08:00
|
|
|
|
| value / v-model | 绑定值,选中选项卡的 name | string | — | 第一个选项卡的 name |
|
2016-11-13 11:58:45 +08:00
|
|
|
|
| type | 风格类型 | string | card/border-card | — |
|
|
|
|
|
| closable | 标签是否可关闭 | boolean | — | false |
|
2017-01-12 22:46:26 +08:00
|
|
|
|
| addable | 标签是否可增加 | boolean | — | false |
|
|
|
|
|
| editable | 标签是否同时可增加和关闭 | boolean | — | false |
|
2017-07-30 16:12:06 +08:00
|
|
|
|
| tab-position | 选项卡所在位置 | string | top/right/bottom/left | top |
|
2018-06-07 11:18:57 +08:00
|
|
|
|
| stretch | 标签的宽度是否自撑开 | boolean | - | false |
|
2018-06-25 15:59:49 +08:00
|
|
|
|
| before-leave | 切换标签之前的钩子,若返回 false 或者返回 Promise 且被 reject,则阻止切换。 | Function(activeName, oldActiveName) | — | — |
|
2016-07-27 14:15:02 +08:00
|
|
|
|
|
2016-08-26 18:34:54 +08:00
|
|
|
|
### Tabs Events
|
2016-08-23 15:24:26 +08:00
|
|
|
|
| 事件名称 | 说明 | 回调参数 |
|
|
|
|
|
|---------- |-------- |---------- |
|
2017-01-12 22:46:26 +08:00
|
|
|
|
| tab-click | tab 被选中时触发 | 被选中的标签 tab 实例 |
|
|
|
|
|
| tab-remove | 点击 tab 移除按钮后触发 | 被删除的标签的 name |
|
|
|
|
|
| tab-add | 点击 tabs 的新增按钮后触发 | — |
|
|
|
|
|
| edit | 点击 tabs 的新增按钮或 tab 被关闭后触发 | (targetName, action) |
|
2016-08-23 15:24:26 +08:00
|
|
|
|
|
2016-08-26 18:34:54 +08:00
|
|
|
|
### Tab-pane Attributes
|
2016-07-27 14:15:02 +08:00
|
|
|
|
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|
|
|
|
|
|---------- |-------- |---------- |------------- |-------- |
|
2016-09-02 13:56:47 +08:00
|
|
|
|
| label | 选项卡标题 | string | — | — |
|
2016-12-29 20:53:33 +08:00
|
|
|
|
| disabled | 是否禁用 | boolean | — | false |
|
2016-09-28 15:20:50 +08:00
|
|
|
|
| name | 与选项卡 activeName 对应的标识符,表示选项卡别名 | string | — | 该选项卡在选项卡列表中的顺序值,如第一个选项卡则为'1' |
|
2016-12-22 00:02:51 +08:00
|
|
|
|
| closable | 标签是否可关闭 | boolean | — | false |
|
2018-05-23 19:04:51 +08:00
|
|
|
|
| lazy | 标签是否延迟渲染 | boolean | — | false |
|