0

I am learning vue3 along with element-plus. While studying these concepts, I found [#default="scope"].

<template>
  <el-table :data="filterTableData" style="width: 100%">
    <el-table-column label="Date" prop="date" />
    <el-table-column label="Name" prop="name" />
    <el-table-column align="right">
      <template #header>
        <el-input v-model="search" size="small" placeholder="Type to search" />
      </template>


      <template #default="scope">


        <el-button size="small" @click="handleEdit(scope.$index, scope.row)"
          >Edit</el-button
        >
        <el-button
          size="small"
          type="danger"
          @click="handleDelete(scope.$index, scope.row)"
          >Delete</el-button
        >
      </template>
    </el-table-column>
  </el-table>
</template>

<script lang="ts" setup>
import { computed, ref } from 'vue'

interface User {
  date: string
  name: string
  address: string
}

const search = ref('')
const filterTableData = computed(() =>
  tableData.filter(
    (data) =>
      !search.value ||
      data.name.toLowerCase().includes(search.value.toLowerCase())
  )
)
const handleEdit = (index: number, row: User) => {
  console.log(index, row)
}
const handleDelete = (index: number, row: User) => {
  console.log(index, row)
}

const tableData: User[] = [
  {
    date: '2016-05-03',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-02',
    name: 'John',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-04',
    name: 'Morgan',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-01',
    name: 'Jessy',
    address: 'No. 189, Grove St, Los Angeles',
  },
]
</script>

Even if I researched the meaning of #default="scope, I couldn't find a good answer. While looking for the meaning of this, I only saw some concepts like slot, but this is used to get data from its parent component, so I don't understand how it is related. scope.row is also used in another example, so probably this is only used for a table.

Can anyone tell me what is 'scope' is doing? If there is a good document to check out, it will be appreciated. Thank you.

koji
  • 23
  • 5

1 Answers1

2

The #default here is indeed a named slot as explained here: https://vuejs.org/guide/components/slots.html#named-slots

Scoped slots can help passing data from child to parent to have some reusability mainly, but regular slots are used to make the inner content of a component more flexible, that why you can put anything inside of your #header like, like the input tag, would be another one otherwise.

Give a read to the whole page because it is indeed what you need to use here, it's more advanced but a lot of libraries are using this approach (since it's a good way of doing things).

UPDATE: and scope in your code snippet, is indeed a named scoped slot default state as explained here: https://vuejs.org/guide/components/slots.html#named-scoped-slots

kissu
  • 40,416
  • 14
  • 65
  • 133