简体   繁体   English

在保持 animation 顺序的同时动态添加 v-tab-item

[英]Dynamically add v-tab-item while maintaining animation order

I am trying to dynamically update the items shown in a Vuetify <v-tabs-items> group, but the animation is not working properly.我正在尝试动态更新 Vuetify <v-tabs-items>组中显示的项目,但 animation 无法正常工作。 When I select a newly added tab, the animation always acts as if that tab were to the right, despite it being to the left in the DOM.当我 select 一个新添加的选项卡时,animation 总是表现得好像该选项卡在右侧,尽管它在 DOM 中位于左侧。 How can I correct this, so the order that the tabs appear in the tab bar reflects the direction of movement?我该如何纠正这个问题,以便选项卡出现在选项卡栏中的顺序反映了移动的方向?

In this example, note that for the original tabs, clicking on a tab further right causes it to be animated in from the right.在此示例中,请注意对于原始选项卡,单击更右侧的选项卡会使其从右侧动画化。 However, after adding a new tab on the left, clicking on this new tab causes it to slide in from the right not the left.但是,在左侧添加新选项卡后,单击此新选项卡会使其从右侧滑入,而不是从左侧滑入。

Sandbox 沙盒

<template>
  <v-app>
    <v-card>
      <v-tabs v-model="chosenTab" grow>
        <v-tab v-for="x in xs" :key="x">
          {{ x }}
        </v-tab>
      </v-tabs>
      <v-tabs-items v-model="chosenTab">
        <v-tab-item v-for="x in xs" :key="x">
          <v-card flat>
            <v-card-text>
              {{ x }}
            </v-card-text>
          </v-card>
        </v-tab-item>
      </v-tabs-items>
      <v-card-actions>
        <v-btn @click="xs.unshift('new-tab-' + xs.length)"> Add new tab </v-btn>
      </v-card-actions>
    </v-card>
  </v-app>
</template>

<script>
export default {
  data() {
    return {
      chosenTab: null,
      xs: ["tab-0", "tab-1", "tab-2"],
    };
  },
};
</script>

You should use href prop on v-tab and id prop on v-tab-item - otherwise it will not work.您应该在v-tab上使用href道具,在v-tab-item上使用id道具 - 否则它将不起作用。 Tabs must have unique string identifiers - relying on the default numeric indexing is error-prone because it (the numeric indexing) depends on the tab's creation order rather than their position in DOM.选项卡必须具有唯一的字符串标识符 - 依赖默认数字索引容易出错,因为它(数字索引)取决于选项卡的创建顺序,而不是它们在 DOM 中的 position。

Every time a new tab is detected by VTabs - it goes through the created() hook in mixins/groupable/index.js which in turn calls register() method in VItemGroup.js .每次 VTabs 检测到新选项卡时 - 它都会通过mixins/groupable/index.js中的created()钩子,然后调用VItemGroup.js中的register()方法。 New tabs are always pushed to the internal array with tabs - so their internal order (which is used by the animation) depends on their creation order.新标签总是被推送到带有标签的内部数组 - 所以它们的内部顺序(由动画使用)取决于它们的创建顺序。

You can not change this behavior unless you patch Vuetify.除非您修补 Vuetify,否则您无法更改此行为。 So in order to overcome it - you must key the v-tab-item by their index rather than by their ID.因此,为了克服它 - 您必须通过索引而不是 ID 来key v-tab-item Yes, this will cause needless rerendering - but this is the price for working around this "wrong" Vuetify behavior until they fix it.是的,这将导致不必要的重新渲染——但这是解决这种“错误”Vuetify 行为的代价,直到他们修复它。

<template>
  <v-app>
    <v-card>
      <v-tabs v-model="chosenTab" grow>
        <v-tab v-for="x in xs" :key="x" :href="'#'+x">
          {{ x }}
        </v-tab>
      </v-tabs>
      <v-tabs-items v-model="chosenTab">
        <v-tab-item v-for="(x,idx) in xs" :key="idx" :id="x">
          <v-card flat>
            <v-card-text>
              {{ x }}
            </v-card-text>
          </v-card>
        </v-tab-item>
      </v-tabs-items>
      <v-card-actions>
        <v-btn @click="xs.unshift('new-tab-' + xs.length)"> Add new tab </v-btn>
      </v-card-actions>
    </v-card>
  </v-app>
</template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM