Skip to content

Commit 7208911

Browse files
authored
fix:修复初始化tab内容无法加载问题 (#3920)
1 parent 2889bcb commit 7208911

File tree

1 file changed

+24
-4
lines changed
  • packages/renderless/src/tabs-mf

1 file changed

+24
-4
lines changed

packages/renderless/src/tabs-mf/vue.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,32 @@ export const renderless = (props, hooks, { vm, emit, nextTick }) => {
9191
(name) => name && api.setActive(name)
9292
)
9393

94+
// 监听 cacheCurrentItem 变化,确保 currentItem 及时更新
95+
watch(
96+
() => state.cacheCurrentItem,
97+
(newItem) => {
98+
if (newItem && newItem !== state.currentItem) {
99+
state.currentItem = newItem
100+
}
101+
},
102+
{ immediate: true }
103+
)
104+
94105
onMounted(() => {
95106
// 在 vue2 类似 tabSwipe0 这些动态的 ref 只能在 nextTick 中拿到
96-
nextTick(() => api.observeTabSwipeSize())
97-
state.currentItem = state.cacheCurrentItem
98-
props.activeName && api.scrollTo(props.activeName)
99-
props.modelValue && api.scrollTo(props.modelValue)
107+
nextTick(() => {
108+
api.observeTabSwipeSize()
109+
// 确保 currentItem 已设置,如果还没有则使用 cacheCurrentItem
110+
if (!state.currentItem && state.cacheCurrentItem) {
111+
state.currentItem = state.cacheCurrentItem
112+
}
113+
// 如果没有 activeName 或 modelValue,且没有选中的 item,则设置第一个 item 为选中
114+
if (!props.activeName && !props.modelValue && state.items.length > 0 && !state.cacheCurrentItem) {
115+
api.changeCurrentName(state.items[0].name)
116+
}
117+
props.activeName && api.scrollTo(props.activeName)
118+
props.modelValue && api.scrollTo(props.modelValue)
119+
})
100120
})
101121

102122
onBeforeUnmount(() => {

0 commit comments

Comments
 (0)