11<template >
22 <view :class =" `wd-tab ${customClass}`" :style =" customStyle" >
3- <view v-if = " painted " class =" wd-tab__body" :style =" tabBodyStyle" >
3+ <view class =" wd-tab__body" v-if = " shouldBeRender " :style =" tabBodyStyle" >
44 <slot />
55 </view >
66 </view >
@@ -25,24 +25,30 @@ import { tabProps } from './types'
2525
2626const props = defineProps (tabProps )
2727
28- const painted = ref <boolean >(false ) // 初始状态tab不会渲染,必须通过tabs来设置painted使tab渲染
29- const isShow = ref <boolean >(false )
3028const { proxy } = getCurrentInstance () as any
3129const { parent : tabs, index } = useParent (TABS_KEY )
3230
3331// 激活项下标
34- const activeIndex = computed (() => {
35- return isDef (tabs ) ? tabs .state .activeIndex : 0
32+ const active = computed (() => {
33+ return isDef (tabs ) ? tabs .state .activeIndex === index . value : false
3634})
3735
36+ const painted = ref <boolean >(active .value ) // 初始状态tab不会渲染,必须通过tabs来设置painted使tab渲染
37+
3838const tabBodyStyle = computed (() => {
3939 const style: CSSProperties = {}
40- if (! isShow .value && (! isDef (tabs ) || ! tabs .props .animated )) {
40+ if (! active .value && (! isDef (tabs ) || ! tabs .props .animated )) {
4141 style .display = ' none'
4242 }
4343 return objToStyle (style )
4444})
4545
46+ const shouldBeRender = computed (() => ! props .lazy || painted .value || active .value )
47+
48+ watch (active , (val ) => {
49+ if (val ) painted .value = true
50+ })
51+
4652watch (
4753 () => props .name ,
4854 (newValue ) => {
@@ -60,18 +66,6 @@ watch(
6066 }
6167)
6268
63- watch (
64- () => activeIndex .value ,
65- (newValue ) => {
66- if (newValue === index .value ) {
67- setShow (true , true )
68- } else {
69- setShow (painted .value , false )
70- }
71- },
72- { deep: true , immediate: true }
73- )
74-
7569/**
7670 * @description 检测tab绑定的name是否和其它tab的name冲突
7771 * @param {Object} self 自身
@@ -88,21 +82,6 @@ function checkName(self: any) {
8882 }
8983 })
9084}
91-
92- /**
93- * 设置子组件展示
94- * @param setPainted
95- * @param setIsShow
96- */
97- function setShow(setPainted : boolean , setIsShow : boolean ) {
98- painted .value = setPainted
99- isShow .value = setIsShow
100- }
101-
102- defineExpose ({
103- setShow ,
104- painted
105- })
10685 </script >
10786<style lang="scss" scoped>
10887@import ' ./index.scss' ;
0 commit comments