我有一个 VueJS/Vuetify 应用程序,它具有使用v-tabs
/v-tab
组件在页面之间导航的标签栏。我已经使用元素中的click
事件实现了代码,该事件v-tab
检查以确保当用户单击另一个选项卡时没有未保存的内容,如果有,则显示一个v-dialog
用于提醒用户的模式。如果用户选择继续,它将继续到所需的选项卡/组件。但是,如果用户Cancel
在模式中进行选择,则页面将留在原来的位置。
这是选项卡组件:
<template>
<div>
<!-- Tabs -->
<v-tabs
color="secondary"
:value="currentTab"
>
<v-tab
v-for="(tab, i) in userTabs"
:key="i"
:href="tab.href"
@click="tabClick(tab.component, tab.link)"
:disabled="isDisabled(tab)"
>
{{ tab.title }}
</v-tab>
</v-tabs>
<BaseConfirmModal
:value="showUnsaved"
:title="unsavedContentTitle"
:text="unsavedContentText"
declineText="Cancel"
@clicked="unsavedModalConfirm"
/>
</div>
</template>
<script>
import { mapGetters, mapActions } from 'vuex';
import baseTabMixin from '@/components/mixins/workspace/baseTabMixin';
export default {
name: 'UserTabs',
data: () => ({
userTabs: [
{
title: 'General Info',
href: '#tab-general',
link: 'tab-general',
component: 'UserEdit',
},
{
title: 'Enrollments',
href: '#tab-enrollments',
link: 'tab-enrollments',
component: 'UserEnrollmentEdit',
},
{
title: 'Alerts',
href: '#tab-alerts',
link: 'tab-alerts',
component: 'UserAlertEdit',
},
{
title: 'Devices',
href: '#tab-devices',
link: 'tab-devices',
component: 'UserDeviceEdit',
},
],
}),
computed: {
...mapGetters('app', ['getStickyTenant', 'roleAtLeastTa', 'getUnsaved']),
...mapGetters('users', ['getCurrent']),
...mapGetters('tabs', {
currentTab: 'getSelected',
}),
},
methods: {
...mapActions('tabs', {
setCurrentTab: 'setSelected',
}),
isDisabled(item) {
if (item.component === 'UserEdit') {
return false;
}
if (item.component === 'UserDeviceEdit' && !this.roleAtLeastTa) {
return true;
}
return !this.getCurrent.userId;
},
},
mixins: [baseTabMixin],
};
</script>
和参考baseTabMixin
:
import { mapGetters, mapActions } from 'vuex';
const baseTabMixin = {
data: () => ({
showUnsaved: false,
unsavedContentTitle: 'Unsaved Changes',
unsavedContentText: 'You have made changes to this page that are not saved. Do you wish to continue?',
destTabComponent: '',
destTabLink: '',
}),
components: {
BaseConfirmModal: () => import('@/components/base/BaseConfirmModal'),
},
computed: {
...mapGetters('app', ['getUnsaved']),
},
methods: {
...mapActions('app', ['setUnsaved']),
tabClick(component, tab) {
// Check to see if getUnsaved === true; if it is,
// set variable to display warning modal.
if (this.getUnsaved) {
this.showUnsaved = true;
} else {
// There is no unsaved content, so continue to the desired tab.
this.destTabComponent = component;
this.destTabLink = tab;
this.setCurrentTab(tab);
this.$router.push({ name: component });
}
},
unsavedModalConfirm(confirm) {
if (confirm) {
this.setCurrentTab(this.destTabLink);
this.$router.push({ name: this.destTabComponent });
}
this.showUnsaved = false;
},
},
};
export default baseTabMixin;
问题与标签项突出显示有关。单击新选项卡时,滑块会移动到新选项卡,并且在tabClick()
调用单击事件(在本例中)之前,新选项卡标题会加粗。当我Cancel
在我的模态中选择时,它会离开页面(如预期的那样),但单击的选项卡仍然突出显示,下方的滑块和粗体文本。由于这一切都发生在我的点击处理程序被调用之前,有没有办法a)在点击事件被调用之前停止突出显示,或者b)将突出显示反转回当前选项卡?