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

ace*_*ace 9 vue.js vuejs2 bootstrap-vue

我正在使用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>
Run Code Online (Sandbox Code Playgroud)

这是猫的路线:

{
        path: '/animals/:id#cats',
        name: 'getCats',
        component: Animals // removed from HTML to simplify
    },
Run Code Online (Sandbox Code Playgroud)

在组件代码中:

this.$router.replace({ name: 'getCats', params: { id: this.$route.params.id }})
Run Code Online (Sandbox Code Playgroud)

这将需要:

本地主机:3000/animals/234909888#cats

但是狗选项卡是打开的(第一个选项卡)而不是猫选项卡。刷新浏览器也会显示空白页面。

如何解决这个问题?

Ric*_*sen 5

您可以尝试添加v-model="tabIndex"<b-tabs>标签,并用于$route.hash将其设置在mounted().

<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>
Run Code Online (Sandbox Code Playgroud)
export default {
  ...
  data() {
    return {
      tabIndex: 0,
      tabs: ['#dogs', '#cats']
    }
  },
  mounted() {
    this.tabIndex = this.tabs.findIndex(tab => tab === this.$route.hash)
  }
  ...
}
Run Code Online (Sandbox Code Playgroud)