如何使用Vuetify V-Data-table项目插槽?

发布于 2025-02-09 04:47:25 字数 640 浏览 2 评论 0原文

<!-- DataTable.vue -->
<template>
  <v-data-table
    class="elevation-1"
    v-bind="$attrs"
    v-on="$listeners"
    dense
  ></v-data-table>
</template>

如您所见,我创建了一个包装&lt; v-data-table&gt;的组件。我想知道,为什么如果使用它,我无法访问项目插槽?

<!-- PeopleView.vue -->
<template>
  <v-container>
    <DataTable
      :headers="headers"
      :items="people"
    >
      <template #item.fullName="{ value }">
        <b>{{ value }}</b>
      </template>
    </DataTable>
  </v-container>
</template>
<!-- DataTable.vue -->
<template>
  <v-data-table
    class="elevation-1"
    v-bind="$attrs"
    v-on="$listeners"
    dense
  ></v-data-table>
</template>

As you can see, I created a component which wraps <v-data-table>. I was wondering, why if I use it I'm not able to access the item slots?

<!-- PeopleView.vue -->
<template>
  <v-container>
    <DataTable
      :headers="headers"
      :items="people"
    >
      <template #item.fullName="{ value }">
        <b>{{ value }}</b>
      </template>
    </DataTable>
  </v-container>
</template>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的呼吸 2025-02-16 04:47:25

内部添加插槽 v-data-table可能可能会解决您的问题。

  <v-data-table
    :class="{ clickable }"
    :headers="reshapedHeaders"
    :items="items"
    :loading="loading"
    :sort-by="sortBy"
    sort-desc
    :item-key="itemKey"
    :expanded.sync="expanded"
    :show-expand="showExpand"
    :hide-default-footer="disablePagination"
    :disable-pagination="disablePagination"
    @click:row="handleClickRow"
    @dblclick:row.stop="handleDblclickRow"
 >
            <!-- Pass on all named slots -->
            <slot
                v-for="slot in Object.keys($slots)"
                :name="slot"
                :slot="slot"
            />

            <!-- Pass on all scoped slots -->
            <template
                v-for="slot in Object.keys($scopedSlots)"
                :slot="slot"
                slot-scope="scope"
            >
                <slot :name="slot" v-bind="scope" />
            </template>
   </v-data-table>

Adding a slot inside v-data-table might probably solve your problem.

  <v-data-table
    :class="{ clickable }"
    :headers="reshapedHeaders"
    :items="items"
    :loading="loading"
    :sort-by="sortBy"
    sort-desc
    :item-key="itemKey"
    :expanded.sync="expanded"
    :show-expand="showExpand"
    :hide-default-footer="disablePagination"
    :disable-pagination="disablePagination"
    @click:row="handleClickRow"
    @dblclick:row.stop="handleDblclickRow"
 >
            <!-- Pass on all named slots -->
            <slot
                v-for="slot in Object.keys($slots)"
                :name="slot"
                :slot="slot"
            />

            <!-- Pass on all scoped slots -->
            <template
                v-for="slot in Object.keys($scopedSlots)"
                :slot="slot"
                slot-scope="scope"
            >
                <slot :name="slot" v-bind="scope" />
            </template>
   </v-data-table>
差↓一点笑了 2025-02-16 04:47:25

要使用项目插槽,您必须“覆盖”插槽和示例插槽。

<!-- DataTable.vue -->
<template>
  <v-data-table
    ...
  >
    <template v-for="(_, name) in $slots">
      <template :slot="name">
        <slot :name="name"></slot>
      </template>
    </template>

    <template
      v-for="(_, name) in $scopedSlots"
      #[name]="data"
    >
      <slot
        :name="name"
        v-bind="data"
      ></slot>
    </template>
  </v-data-table>
</template>

To use item slots you must "override" slots and scoped slots.

<!-- DataTable.vue -->
<template>
  <v-data-table
    ...
  >
    <template v-for="(_, name) in $slots">
      <template :slot="name">
        <slot :name="name"></slot>
      </template>
    </template>

    <template
      v-for="(_, name) in $scopedSlots"
      #[name]="data"
    >
      <slot
        :name="name"
        v-bind="data"
      ></slot>
    </template>
  </v-data-table>
</template>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文