Search code examples
javascriptvue.jsslot

Passing in prop in scoped-slot not working


I am trying to wrap my table inside of a slot for reusability purposes but I am having trouble with passing in my label prop:

My main parent component Parent.vue:

    <Table
        :headers="headers"
        :items="people"
        :search="search"
        :label="label"
    >
        <template scope="props">
        Label is: {{ label }} <!-- this label is not printing out -->
        <v-data-table :headers="props.headers" :items="people" :search="props.search">
            <template slot="items" slot-scope="props">
            <td>{{ props.item.firstName }}</td>
            <td>{{ props.item.lastName }}</td>
            <td>{{ props.item.email }}</td>
            </template>
        </v-data-table>
        </template>
    </Table>

My Table component:

<template>
  <div>
    <slot :items="items" :headers="headers" :search="search" scoped-slot="{label: label}"></slot>
  </div>
</template>

<script>
  export default {
    data() {
      return {
          label: "Some label"
      }
    },
    computed: {

    },
    props: ["items", "headers", "search"]
  }

</script>

This method gives me error Property or method "label" is not defined. Could someone help point out where I am going wrong with passing in the label prop?


Solution

  • You need to access it as props.label instead of just label

    <template slot-scope="slotProps">
      Label is: {{ slotProps.label }} <!-- this label is not printing out -->
      <v-data-table :headers="props.headers" :items="people" :search="props.search">
          <template slot="items" slot-scope="props">
          <td>{{ props.item.firstName }}</td>
          <td>{{ props.item.lastName }}</td>
          <td>{{ props.item.email }}</td>
          </template>
      </v-data-table>
    </template>
    

    and in Table component, passing label as simple as other binding value:

    <template>
      <div>
        <slot :items="items" :headers="headers" :search="search" :label="label"></slot>
      </div>
    </template>