elementui tabs动态标签

渲染已有标签(content为组件的方式引入)和 增加tab

<template>
  <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick" editable @edit="handleTabsEdit">
    <el-tab-pane v-for="item in editableTabs" :key="item.name" :label="item.title" :name="item.name">
      <component :is="item.content" :objId="item.name"></component>
    </el-tab-pane>
  </el-tabs>
</template>
<script  setup>
import { ref, shallowRef, defineAsyncComponent } from 'vue'
import firstContentVue from './tabs/firstContent.vue';
import secondContentVue from './tabs/secondContent.vue';
const activeName = ref('first')
const name = ref('Rich')
const index = ref(0)
const editableTabs = ref([
  {
    title: 'User',
    name: 'first',
    content: shallowRef(firstContentVue),
  },
  {
    title: 'Config',
    name: 'second',
    content: shallowRef(secondContentVue),
  },
])

const handleClick = (event) => {
  console.log(event)
}
const handleTabsEdit = (targetName,action) => {
  console.log(targetName)
  console.log(action)
  if (action === 'add') {
    const i = name.value === 'Rich' ? name.value : 'Table'
    index.value++
    const newTableTitle = `New Tab${index.value}`
    const newTabName = `third${index.value}`
    editableTabs.value.push({
      title: newTableTitle,
      name: newTabName,
      content: shallowRef(defineAsyncComponent(() => import(`./tabs/public${i}.vue`))),
    })
    activeName.value = newTabName
  }
}
</script>
<style scoped>
.demo-tabs > .el-tabs__content {
  padding: 32px;
  color: #6b778c;
  font-size: 32px;
  font-weight: 600;
}
</style>

简单说明:
(1)shallowRef 是为解决下面这个错误的

Vue received a Component which was made a reactive object. This can lead to unnecessary performance overhead, and should be avoided by marking the component with `markRaw` or using `shallowRef` instead of `ref`. 
Component that was made reactive: 
Vue接收到一个组件,该组件被设置为反应对象。这可能导致不必要的性能开销,应通过使用“markRaw”标记组件或使用“shallowRef”而不是“ref”来避免。

活性成分:

(2)defineAsyncComponent是为解决下面的错误

[Vue warn]: Invalid VNode type: undefined (undefined)
[Vue warn]:无效的VNode类型:未定义(未定义)

(3)代码中public组件 中包含一个input和一个button ,经测试,相同组件,新增加的tab,input 输入不同的值,tab页是不同的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值