【发布时间】:2020-09-02 07:22:38
【问题描述】:
早上好,
我的目标如下:
每次打开新标签页时,它都会变为“活动”状态,并且浏览器会显示其内容
- 问题:每次打开新选项卡时,之前打开的选项卡都保持“活动”状态。
Look at a trivial example of the problem.
为了重现问题:
- 访问BootstrapVue Playground;
- 滚动到页面底部,在标题为“动态选项卡 + 选项卡末端槽”的部分中(在高级示例部分中提供);
- 在代码区双击(可以修改);
- 粘贴以下代码
<template>
<div>
<b-card no-body>
<b-tabs card>
<!-- Render Tabs, supply a unique `key` to each tab -->
<b-tab v-for="tab in tabs" :key="tab.id" :class="{active:tab.isActive}" :title="'Tab ' + tab.id">
Tab contents {{ tab.id }}
<b-button size="sm" variant="danger" class="float-right" @click="closeTab(tab.id)">
Close tab
</b-button>
</b-tab>
<!-- New Tab Button (Using tabs-end slot) -->
<template v-slot:tabs-end>
<b-nav-item role="presentation" @click.prevent="newTab" href="#"><b>+</b></b-nav-item>
</template>
<!-- Render this if no tabs -->
<template v-slot:empty>
<div class="text-center text-muted">
There are no open tabs<br>
Open a new tab using the <b>+</b> button above.
</div>
</template>
</b-tabs>
</b-card>
</div>
</template>
<script>
export default {
data() {
return {
tabs: [],
tabCounter: 0,
activeTab: {}
}
},
methods: {
closeTab(x) {
for (let i = 0; i < this.tabs.length; i++) {
if (this.tabs[i].id === x) {
this.tabs.splice(i, 1)
}
}
},
newTab() {
const newTab = {
id: this.tabCounter,
isActive: true
};
this.tabCounter++;
this.tabs.push(newTab);
this.setActive(newTab);
},
setActive(tab) {
tab.isActive = true;
this.activeTab = tab;
this.tabs.forEach((tab) => {
if (tab.id !== this.activeTab.id) {
tab.isActive = false;
}
})
}
}
}
</script>
- 尝试通过单击呈现区域中的“+”按钮添加新选项卡,该按钮位于粘贴代码的开头。
我做错了什么?
【问题讨论】:
-
我已尝试修改此example fiddle with jQuery 中显示的示例。
标签: javascript vue.js tabs bootstrap-vue