【问题标题】:Bootstrap vue Tab component is not working when an Index is used to navigate trough the tabs当使用索引导航选项卡时,Bootstrap vue Tab 组件不起作用
【发布时间】:2021-09-15 11:57:54
【问题描述】:

为什么这个非常简单的 vue 引导脚本不起作用,在按下切换按钮时它不会增加 tabIndex 的值。

  • tabIndex从0开始,用户按下tabIndex,值还是0。

  • 如果 tabIndex 从 disabled 属性中删除,它将起作用,但不会达到我要求的目的。 脚本可以在这里看到: https://jsfiddle.net/Xarina/tkoyph4x/1/

    <div id="app">
      <b-card no-block>{{tabIndex}}
       <b-tabs small card ref="tabs" v-model="tabIndex">
         <b-tab title="General">
           I'm the first fading tab
         </b-tab>
         <b-tab title="Edit profile" :disabled="tabIndex < 1">
           I'm the second tab
         </b-tab>
         <b-tab title="Premium Plan" :disabled="tabIndex < 2">
           Sibzamini!
         </b-tab>
       </b-tabs>
      </b-card>
    
     <button @click="tabIndex++">
      Click to toggle disable
     </button>
    </div>
    

【问题讨论】:

    标签: vue.js bootstrap-vue


    【解决方案1】:

    您可以根据tabIndex 使用title-item-class 将自定义类绑定到每个tab

    window.app = new Vue({
      el: '#app',
      data: () => ({ tabs: [], tabCounter: 0, disabled: false, tabIndex: 0 }),
    });
    .disabledTab .nav-link {  pointer-events: none; color: #666666; }
    <link type="text/css" rel="stylesheet" href="https://unpkg.com/bootstrap/dist/css/bootstrap.min.css"/>
    <link type="text/css" rel="stylesheet" href="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.css"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.min.js"></script>
    <script src="https://unpkg.com/babel-polyfi0ll@latest/dist/polyfill.min.js"></script>
    <script src="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.js"></script>
    
    <div id="app">
      <b-card no-block>{{tabIndex}}
        <b-tabs small card ref="tabs" v-model="tabIndex">
          <b-tab title="General">I'm the first fading tab</b-tab>
          <b-tab title="Edit profile" :title-item-class="{ disabledTab: tabIndex < 1 }">I'm the second tab</b-tab>
          <b-tab title="Premium Plan" :title-item-class="{ disabledTab: tabIndex < 2 }">Sibzamini!</b-tab>
        </b-tabs>
      </b-card>
      <button @click="tabIndex++">Click to toggle disable</button>
    </div>

    【讨论】:

    • 是的,我想我可能不得不遵循这种方法,我在想有一些引导 vue 的事情我可以很容易地做到。谢谢你
    • 问题是,当一个选项卡被禁用时,它的状态不会随着状态 tabIndex 的变化而改变,因此它在逻辑上不在选项卡数组中并且单击按钮不会将索引更改为 0 以上因为选项卡数组只有一个活动选项卡。以上是在从 bootstrap-vue 中找不到明确方法后提出的建议,它为您的问题提供了类似的解决方案。
    • 是的,我试过了,起初它没有用,但我犯了一个错误,然后它工作了,不知道指针事件:无;太棒了!
    【解决方案2】:

    发生这种情况是因为您无法切换到已禁用的标签。并且检查发生在索引被禁用之前,这意味着它将选项卡索引设置回0。

    您可以通过使用两个数据属性来解决此问题,一个用于禁用选项卡,一个用于&lt;b-tabs&gt; v-model。然后首先使用 for 禁用更新属性,并在 $nextTick 回调中更新 v-model 属性。

    在下面的示例中,我使用watcher 在其他属性更改时自动设置第二个属性。

    new Vue({
      el: '#app',
      data() {
        return {
          currentTab: 0,
          disabledTabIndex: 0
        }
      },
      watch: {
        disabledTabIndex(newVal) {
          this.$nextTick(() => {
            this.currentTab = newVal;
          })
        }
      }
    });
    <link href="https://unpkg.com/bootstrap@4.5.3/dist/css/bootstrap.min.css" rel="stylesheet" />
    <link href="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.css" rel="stylesheet" />
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.11/vue.min.js"></script>
    <script src="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.js"></script>
    
    <div id="app">
      <b-card no-block>
        Current tab: {{ currentTab }}
        <b-tabs small card ref="tabs" v-model="currentTab">
          <b-tab title="General">I'm the first fading tab</b-tab>
          <b-tab title="Edit profile" :disabled="disabledTabIndex < 1">
            I'm the second tab
          </b-tab>
          <b-tab title="Premium Plan" :disabled="disabledTabIndex < 2">
            Sibzamini!
          </b-tab>
        </b-tabs>
      </b-card>
      <button @click="disabledTabIndex++">Click to toggle disable</button>
    </div>

    【讨论】:

    • 有趣的解决方案,我已经使用了另一个。但这是有道理的。谢谢
    猜你喜欢
    • 2022-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    • 2017-10-26
    • 2022-06-17
    • 2021-04-03
    • 1970-01-01
    相关资源
    最近更新 更多