如何导航到 vue 路由中 Bootstrap-vue 选项卡中的特定选项卡?

How to navigate to specific tab in Bootstrap-vue tabs in vue routes?

我正在使用 Bootstrap-vue tabs。这是 HTML 选项卡:

<b-tabs>
  <b-tab title="Exotic Dogs" href="#dogs">
    <br>Dogs here
  </b-tab>
  <b-tab title="Exotic Cats"  href="#cats">
    <br>Cats here
  </b-tab>

</b-tabs>

这是猫的路线:

{
        path: '/animals/:id#cats',
        name: 'getCats',
        component: Animals // removed from HTML to simplify
    },

在组件代码中:

this.$router.replace({ name: 'getCats', params: { id: this.$route.params.id }})

这需要:

localhost:3000/animals/234909888#cats

但是打开了狗标签(第一个标签)而不是猫标签。刷新浏览器也会显示空白页面。

如何解决这个问题?

您可以尝试将v-model="tabIndex"添加到<b-tabs>标签中,并使用$route.hashmounted()中设置它。

<b-tabs v-model="tabIndex">
  <b-tab title="Exotic Dogs" href="#dogs">
    <br>Dogs here
  </b-tab>
  <b-tab title="Exotic Cats"  href="#cats">
    <br>Cats here
  </b-tab>

</b-tabs>
export default {
  ...
  data() {
    return {
      tabIndex: 0,
      tabs: ['#dogs', '#cats']
    }
  },
  mounted() {
    this.tabIndex = this.tabs.findIndex(tab => tab === this.$route.hash)
  }
  ...
}