antd vue Tabs控件的使用

Ant Design Vue-------Tabs标签页

今天就讲讲Ant Design Vue下的控件----tabs 标签页

结合项目中的需求,讲一下该控件如何使用,需求:

(1)竖排样式
(2)如何使用v-for绑定数据源
(3)change事件
(4)动态生成、动态切换(子组件和component、keepAlive、nextTick的联合使用)

官网案例来一个:

<template>
  <a-tabs v-model:activeKey="activeKey">
    <a-tab-pane key="1" tab="Tab 1">Content of Tab Pane 1</a-tab-pane>
    <a-tab-pane key="2" tab="Tab 2" force-render>Content of Tab Pane 2</a-tab-pane>
    <a-tab-pane key="3" tab="Tab 3">Content of Tab Pane 3</a-tab-pane>
  </a-tabs>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
const activeKey = ref('1');
</script>

效果如图:

项目中的代码:

<div style="height: calc(100vh - 230px)">
          <a-tabs v-model:activeKey="activeItem" tab-position="left" @change="handleTabChange">
            <a-tab-pane v-for="item in itemSource" :tab="item.dictionaryMenuName" :disabled="item.isHas == 1 ? false : true" :key="item.dictionaryName.split('|')[0]" />
          </a-tabs>
        </div>

注:div的样式控制垂直滚动条;

1.tab-position=left控制竖排样式。这里顺便把常用的属性设置也列出来:

(1)v-model:activeKey 绑定的是用户当前选择的tab的key值;

(2)tab-position:共4个位置;top默认顶端;left:左侧竖排展示;right:右侧竖排展示;bottom:底部,如图:

(3)size:共三种大小;small:小,large:大,middle:默认值,如下图:

(4)type:页签的基本样式,三个类型。line、card、editable-card

(6)class样式:

:deep(.ant-tabs-left > .ant-tabs-nav .ant-tabs-tab) {

    width: 265px;

    padding: 2px 20px;

    margin: 2px 0 0;

  }

2.绑定数据源

Tabs.TabPane控件a-tab-pane元素和for搭配使用:

itemSource:数据源(数组)

 item:对象

tab:选项卡的标题;key:唯一标识,对应activeKey;

disabled:根据flag指定该标签是否可用。

3. script setup lang="ts"  和 tabs的change事件

import { onMounted, ref, reactive, toRaw, toRef, computed } from 'vue';

const activeItem = ref('');
const itemSource = ref([]);

function queryTabsList() {
    let queryJson = {};//往后端传值
    Query(queryJson).then((res) => {//Query:API
      itemSource.value = res;
      activeItem.value = null;//默认选项卡都未选中
}
//tabs选择事件
  async function handleTabChange(key) {
    console.log('key',key);
    //var arr = key.split('|');
    //if (arr.length > 0) {
    //    let selItem = arr[0]; //英文名称
    //    let dtoName = arr[1];//中文名称
    //  ……
    //}
}

4. 下面讲讲更复杂的应用,这种场景,一般用于tabs菜单动态切换功能。

(1)模板

<template>
      <div class="bottom-main">
        <a-tabs v-model:activeKey="activeKey" tabPosition="top" @change="tabItemSelected" size="small" type="card" style="height: 40px">
          <a-tab-pane v-for="item in menuTabs" :key="`${item?.value}`" :tab="item?.label" />
        </a-tabs>
        <KeepAlive v-if="isAlive">
          <component :is="currentPage" :param-condition="whereCond" />
        </KeepAlive></div
    ></template>

(2)脚本

const activeKey = ref('');
  const menuTabs = ref([]);
  function initTab() {
    let query = {
      name: 'getClass',
      queryParams: {},
    };
    Query(cbxQuery).then((res) => {//Query:API
      menuTabs.value = res;
    });
  }

//控制是否强制刷新
const isAlive = ref(true);
const currentPage = ref();//当前组件
const whereCond = reactive({//当前页面传值给子组件的参数
    proId: '', //项目名称
  });
//模拟的一组子组件
const typeCompentMap = {
    base: input1,//引入input1组件
    invest: input2,//引入input2组件
    product: input3,//引入input3组件
    price: input4,//引入input4组件
  };
//tab切换事件
function tabItemSelected() {
    currentPage.value = typeCompentMap[activeKey.value];
  }
//挂载事件
onMounted(() => {
    initTab();
  });
//查询事件时刷新tab子组件
  function handleSearch() {
    activeKey.value = 'base';
    whereCond.proId = selectedProjectId.value; 
    if (whereCond.proId) {
      refreshChild();
    }
  }
  function refreshChild() {
    isAlive.value = false;
    currentPage.value = typeCompentMap[activeKey.value];
    nextTick(() => {//利用nextTick更新机制,强制刷新页面
      isAlive.value = true;
    });
  }

vue 中我们改变数据时不会立即触发视图,如果需要实时获取到最新的DOM,这个时候可以手动调用 nextTick

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ant Design Vue 中的 Tabs 组件可以与 Vue Router 配合使用,实现标签栏的功能。具体步骤如下: 1. 在路由配置中,添加 `meta` 字段用于标识当前路由是否需要在标签栏中显示,例如: ```javascript const routes = [ { path: '/', name: 'Home', component: Home, meta: { title: '首页', keepAlive: true, // 是否缓存组件 showTab: true, // 是否在标签栏中显示 }, }, // ... ]; ``` 2. 在 App.vue 中,使用 Tabs 组件来渲染标签栏,并使用 `router-view` 组件来渲染当前路由对应的组件: ```html <template> <div> <a-tabs v-model:selectedTabKey="selectedTabKey" type="editable-card" hide-add @edit="handleTabEdit" style="margin: 0 24px;"> <a-tab-pane v-for="(tab, index) in tabs" :key="tab.key" :tab="tab.title" :closable="index !== 0" @close="handleTabClose(index)"> </a-tab-pane> </a-tabs> <router-view /> </div> </template> <script> export default { data() { return { selectedTabKey: '/', tabs: [ { key: '/', title: '首页', }, ], }; }, created() { const { path, meta } = this.$route; if (meta.showTab) { this.selectedTabKey = path; this.addTab(path, meta.title); } }, methods: { addTab(key, title) { const index = this.tabs.findIndex((tab) => tab.key === key); if (index === -1) { this.tabs.push({ key, title, }); } }, removeTab(key) { const index = this.tabs.findIndex((tab) => tab.key === key); if (index !== -1) { this.tabs.splice(index, 1); } }, handleTabClose(index) { const { key } = this.tabs[index]; this.removeTab(key); this.$router.replace(this.tabs[this.tabs.length - 1].key); }, handleTabEdit(targetKey, action) { if (action === 'add') { this.$router.push(targetKey); } else if (action === 'remove') { this.handleTabClose(this.tabs.findIndex((tab) => tab.key === targetKey)); } }, }, }; </script> ``` 在这个示例中,我们使用了 `selectedTabKey` 属性来绑定当前选中的标签页,使用 `tabs` 数组来存储所有已打开的标签页。在 `created` 钩子函数中,我们通过判断当前路由的 `meta.showTab` 字段来决定是否需要添加标签页。在 `addTab` 方法中,我们使用 `tabs` 数组来存储已打开的标签页,防止重复添加。在 `removeTab` 方法中,我们使用 `tabs` 数组来删除已关闭的标签页。在 `handleTabEdit` 方法中,我们通过判断用户的操作来决定是添加标签页还是关闭标签页。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值