我只是让老的选项API答案对你来说可行
这是代码
<template>
<TabView
v-model:activeIndex="activeIndex"
@tab-change="handleTabChange"
@tab-click="handleTabClick"
>
<TabPanel header="Opening Hours"> Opening hours form goes here </TabPanel>
<TabPanel header="Contacts"> Contacts form goes here </TabPanel>
</TabView>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import TabView from 'primevue/tabview';
import TabPanel from 'primevue/tabpanel';
import type { TabViewChangeEvent, TabViewClickEvent } from 'primevue/tabview';
// Override the default onTabClick method of TabView
// This should ideally be in a global setup file, not in a component
TabView.methods.onTabClick = function (event, tab, i) {
this.$emit('tab-click', {
originalEvent: event,
tabInfo: tab,
index: i,
});
};
// Ref to keep track of the active tab index
const activeIndex = ref();
// Handler for tab click events
const handleTabClick = async (event: TabViewClickEvent) => {
console.log('handleTabClick', event);
const nextTabIndex = event.index;
// Simulated form validation
// In a real scenario, this would be replaced with actual form validation logic
const isValid = false;
if (isValid) {
// If the form is valid, allow the tab change
console.log('form valid');
activeIndex.value = nextTabIndex;
} else {
// If the form is invalid, prevent the tab change
console.log('form invalid');
// No action needed here as we're not changing activeIndex
}
};
// Handler for tab change events
const handleTabChange = async (event: TabViewChangeEvent) => {
console.log('handleTabChange', event);
// Prevent the default tab change behavior
event.originalEvent.preventDefault();
const nextTabIndex = event.index;
// Simulated form validation
// In a real scenario, this would be replaced with actual form validation logic
const isValid = false;
if (isValid) {
// If the form is valid, allow the tab change
console.log('form valid');
activeIndex.value = nextTabIndex;
} else {
// If the form is invalid, prevent the tab change
console.log('form invalid');
// No action needed here as we've already prevented the default behavior
}
};
</script>