如何在自定义组件内使用 Vuetify 3 的数据表格 (data-table) v-slot 功能?

huangapple go评论93阅读模式
英文:

How to use vuetify 3 data-table v-slot functionalities inside a custom component?

问题

Custom Component: @/components/MyDataTable.vue

<template>
  <div>
    <v-data-table :headers="headers" :items="items"> </v-data-table>
  </div>
</template>

<script>
export default {
  props: {
    items: {
      type: Array,
      default: () => [],
    },
    headers: {
      type: Array,
      default: () => [],
    },
  },
};
</script>

App.vue

<template>
  <v-app>
    <v-main>
      <my-data-table :headers="headers" :items="items" item-value="name">
        <template v-slot:[`column.name`]="{ column }">
          {{ column }}
        </template>
        <template v-slot:[`item.name`]="{ item }">
          {{ item.props }}
        </template>
      </my-data-table>
    </v-main>
  </v-app>
</template>

<script>
import MyDataTable from "@/components/MyDataTable.vue";

export default {
  components: {
    MyDataTable,
  },
  data() {
    return {
      headers: [
        { title: "Dessert", align: "start", key: "name" },
        { title: "Calories", align: "end", key: "calories" },
        { title: "Fat (g)", align: "end", key: "fat" },
      ],
      items: [
        {
          name: "Frozen Yogurt",
          calories: 159,
          fat: 6.0,
        },
        {
          name: "Jelly bean",
          calories: 375,
          fat: 0.0,
        },
      ],
    };
  },
};
</script>

如果我将它们放在原生组件中,它们可以工作,但如果我将它们放在自定义组件中,它们不能工作。

期望结果:
如何在自定义组件内使用 Vuetify 3 的数据表格 (data-table) v-slot 功能?

实际结果:
如何在自定义组件内使用 Vuetify 3 的数据表格 (data-table) v-slot 功能?

我使用的是Vue 3和Vuetify 3。

这里是CodeSandbox准备链接

英文:

I want to get v-data-table items and headers using slots from my custom component.

Custom Component: @/components/MyDataTable.vue

&lt;template&gt;
  &lt;div&gt;
    &lt;v-data-table :headers=&quot;headers&quot; :items=&quot;items&quot;&gt; &lt;/v-data-table&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
export default {
  props: {
    items: {
      type: Array,
      default: () =&gt; [],
    },
    headers: {
      type: Array,
      default: () =&gt; [],
    },
  },
};
&lt;/script&gt;

App.vue

&lt;template&gt;
  &lt;v-app&gt;
    &lt;v-main&gt;
      &lt;my-data-table :headers=&quot;headers&quot; :items=&quot;items&quot; item-value=&quot;name&quot;&gt;
        &lt;template v-slot:[`column.name`]=&quot;{ column }&quot;&gt;
          {{ column }}
        &lt;/template&gt;
        &lt;template v-slot:[`item.name`]=&quot;{ item }&quot;&gt;
          {{ item.props }}
        &lt;/template&gt;
      &lt;/my-data-table&gt;
    &lt;/v-main&gt;
  &lt;/v-app&gt;
&lt;/template&gt;

&lt;script&gt;
import MyDataTable from &quot;@/components/MyDataTable.vue&quot;;

export default {
  components: {
    MyDataTable,
  },
  data() {
    return {
      headers: [
        { title: &quot;Dessert&quot;, align: &quot;start&quot;, key: &quot;name&quot; },
        { title: &quot;Calories&quot;, align: &quot;end&quot;, key: &quot;calories&quot; },
        { title: &quot;Fat (g)&quot;, align: &quot;end&quot;, key: &quot;fat&quot; },
      ],
      items: [
        {
          name: &quot;Frozen Yogurt&quot;,
          calories: 159,
          fat: 6.0,
        },
        {
          name: &quot;Jelly bean&quot;,
          calories: 375,
          fat: 0.0,
        },
      ],
    };
  },
};
&lt;/script&gt;

If I put them in the native component they work, but if I put them in my own they do not work.

Expected Result:
如何在自定义组件内使用 Vuetify 3 的数据表格 (data-table) v-slot 功能?

Active Result:
如何在自定义组件内使用 Vuetify 3 的数据表格 (data-table) v-slot 功能?

I use vue 3 with vuetify 3.

Here is the codesandbox ready link.

答案1

得分: 0

尝试通过迭代来转发来自v-datatable的插槽,然后创建可以在父组件中访问的新插槽:

<v-data-table :headers="headers" :items="items">
   <template v-for="(_, scopedSlotName) in $scopedSlots" v-slot:[scopedSlotName]="slotData">
        <slot :name="scopedSlotName" v-bind="slotData" />
   </template>
</v-data-table>
英文:

Try to forward the slots from the v-datatable by iterating over them, then creating new ones that can be accessed in parent component :

&lt;v-data-table :headers=&quot;headers&quot; :items=&quot;items&quot;&gt; 
   &lt;template v-for=&quot;(_, scopedSlotName) in $scopedSlots&quot; v-slot:[scopedSlotName]=&quot;slotData&quot;&gt;
        &lt;slot :name=&quot;scopedSlotName&quot; v-bind=&quot;slotData&quot; /&gt;
   &lt;/template&gt;
&lt;/v-data-table&gt;

huangapple
  • 本文由 发表于 2023年3月12日 19:49:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定