element-plus e-tabs与pinia 一起使用的问题

问题描述

txt 复制代码
pinia中的数据更新了,页面还是旧数据

前端代码

html 复制代码
<template>
  <Layout>
    <section class="_main_">
      <el-tabs editable closable v-model="activeTabName"  type="card">
        <template #add-icon>
          <el-button type="primary" @click="handleAddTab">新增</el-button>
          <el-button type="primary" @click="handleAddTab">绑定</el-button>
        </template>
        <template v-if="tabs.length">
          <el-tab-pane v-for="item in tabs" :key="item.id" :name="item.id">
            <template #label>
              <div class="tab-label">
                <span>{{ item.title }}</span>
                <el-icon @click="removeTab(item.id)"><Close /></el-icon>
              </div>
            </template>
            <component :is="TabContent" v-bind="item" />
          </el-tab-pane>
        </template>
        <el-tab-pane name="default" v-else>
          <template #label>
            <div class="tab-label">
              <span>item</span>
            </div>
          </template>
          <TabContent />
        </el-tab-pane>
      </el-tabs>
    </section>
  </Layout>
</template>

<script lang="ts" setup>
import Layout from "@/layout/index.vue";
import { useTabsStore } from "@/store";
import TabContent from "./components/tabContent/index.vue";
const store = useTabsStore();
const { addTab, removeTab } = store;
const { tabs, activeTabName } = storeToRefs(store);

watchEffect(() => {
  console.log(`activeTabName ==>`, activeTabName.value);
});
const handleAddTab = () => {
  const index = tabs.value.length + 1;
  addTab({ title: `${index}` });
};

</script>

pinia

typescript 复制代码
import { defineStore } from "pinia";
import { v4 as uuid } from "uuid";
type IState = {
  activeTabName: string;
  tabs: {
    title: string;
    id: string;
  }[];
};

const defaultActiveTabName = "default";

const state = reactive<IState>({
  activeTabName: defaultActiveTabName,
  tabs: [],
});

export const useTabsStore = defineStore("tabs", {
  state: () => state,
  actions: {
    /**
     * 新增tab标签页
     */
    addTab(tab: Partial<IState["tabs"][number]>) {
      const id = uuid();
      this.tabs.push({ title: tab.title!, id });
      this.activeTabName = id;
    },
    /**
     * 删除tab标签页
     */
    removeTab(id: string) {
      const index = this.tabs.findIndex((i) => i.id == id);
      this.tabs.splice(index, 1);

      requestAnimationFrame(() => {
        // 更新逻辑
        if (!this.tabs.length) {
          this.activeTabName = defaultActiveTabName;
          return;
        }

        // 判断是否有 activeTabName 绑定的值
        const hasActiveTabName =
          this.tabs.findIndex((i) => i.id == this.activeTabName) >= 0;
        if (!hasActiveTabName) {
          this.activeTabName = this.tabs[this.tabs.length - 1].id;
        }
      });
    },
  },
});

实现方案

typescript 复制代码
...
   /**
     * 删除tab标签页
     */
    removeTab(id: string) {
      const index = this.tabs.findIndex((i) => i.id == id);
      this.tabs.splice(index, 1);

      requestAnimationFrame(() => {
        // 更新逻辑
        if (!this.tabs.length) {
          this.activeTabName = defaultActiveTabName;
          return;
        }

        // 判断是否有 activeTabName 绑定的值
        const hasActiveTabName =
          this.tabs.findIndex((i) => i.id == this.activeTabName) >= 0;
        if (!hasActiveTabName) {
          this.activeTabName = this.tabs[this.tabs.length - 1].id;
        }
      });
    },

使用 requestAnimationFrame可以解决(setTimeout也行,只是有瞬间白屏)

ok ,解决

相关推荐
蓝婷儿1 小时前
第二章:CSS秘典 · 色彩与布局的力量
前端·css
Wyc724092 小时前
HTML:入门
前端·html
Sunny_lxm2 小时前
自定义列甘特图,原生开发dhtmlxgantt根特图,根据数据生成只读根特图,页面展示html demo
前端·html·甘特图·dhtmlxgantt
熊猫钓鱼>_>3 小时前
建筑IT数字化突围:建筑设计企业的生存法则重塑
前端·javascript·easyui
GISer_Jing5 小时前
前端性能指标及优化策略——从加载、渲染和交互阶段分别解读详解并以Webpack+Vue项目为例进行解读
前端·javascript·vue
不知几秋5 小时前
数字取证-内存取证(volatility)
java·linux·前端
水银嘻嘻6 小时前
08 web 自动化之 PO 设计模式详解
前端·自动化
Zero1017138 小时前
【详解pnpm、npm、yarn区别】
前端·react.js·前端框架
&白帝&8 小时前
vue右键显示菜单
前端·javascript·vue.js
Wannaer8 小时前
从 Vue3 回望 Vue2:事件总线的前世今生
前端·javascript·vue.js