vue插槽(slot)详解及element-ui表格操作列的插槽使用解析

一、插槽的概念

组件标签中的内容,是不会显示在页面中的,要想显示出来,就要使用插槽

js 复制代码
<template>
  <div class="child">
    child组件
    <slot />
  </div>
</template>

当在子组件中写上<slot />标签,书写在<Child></Child>中的内容便会在这个位置展示出来

二、具名插槽和匿名插槽

Child.vue

js 复制代码
<template>
  <div class="child">
    <header>
      <slot name="header" />
    </header>
    <main>
      <slot />
    </main>
    <footer>
      <slot name="footer" />
    </footer>
  </div>
</template>

Parent.vue

js 复制代码
<template>
  <div class="parent">
    parent组件
    <Child>
      <h1 slot="header">标题</h1>
      <p>内容</p>
      <p slot="footer">底部</p>
    </Child>
  </div>
</template>

slot标签的name属性就是具名插槽,否则就是匿名插槽,如果需要让某个内容显示在固定的位置上,可以考虑使用具名插槽

2.6.0+版本,父组件中可以这样写:

js 复制代码
      <template v-slot:footer>
        <p>底部</p>
      </template>

但不能将v-slot:footer直接定义在p标签上

还可以使用#语法糖(v-slot:可以简写成#):

js 复制代码
      <template #footer>
        <p>底部</p>
      </template>

三、插槽作用域

1、插槽作用域的作用:子组件可以传值给父组件

2、匿名插槽作用域传值

Child.vue

js 复制代码
    <main>
      <slot :params="{ name: '小明', age: 18 }" />
    </main>

Parent.vue

老的写法

js 复制代码
      <template slot-scope="scope">
        <p>
          内容
          {{ scope.params.name }}--{{ scope.params.age }}
        </p>
      </template>

2.6.0+ slot-scope可以写成v-slot

js 复制代码
      <template v-slot="scope">
        <p>
          内容
          {{ scope.params.name }}--{{ scope.params.age }}
        </p>
      </template>

scope是自定义的变量名,可以用其他名字,也可以使用解构赋值

js 复制代码
      <template v-slot="{ params }">
        <p>
          内容
          {{ params.name }}--{{ params.age }}
        </p>
      </template>

3、具名插槽作用域传值

Child.vue

js 复制代码
    <header>
      <slot name="header" :params="{ name: '小明', age: 18 }" />
    </header>

Parent.vue

老的写法

js 复制代码
      <template slot="header" slot-scope="scope">
        <h1>标题</h1>
        {{ scope.params.name }}--{{ scope.params.age }}
      </template>

2.6.0+

js 复制代码
      <template v-slot:header="{ params }">
        <h1>标题</h1>
        {{ params.name }}--{{ params.age }}
      </template>

使用#简写:

js 复制代码
      <template #header="{ params }">
        <h1>标题</h1>
        {{ params.name }}--{{ params.age }}
      </template>

四、解读el-table中使用的插槽

这里使用slot-scope="scope",说明el-table-column组件中通过slot标签传递了一些值供父组件使用

咱们来模拟一下:

效果:

Parent.vue

js 复制代码
<template>
  <div class="parent">
    parent组件
    <Child :tableData="tableData">
      <template v-slot="scope">
        <el-button @click="handleClick(scope.row)">查看</el-button>
        <el-button>编辑</el-button>
      </template>
    </Child>
  </div>
</template>
<script>
import Child from '../Child'
export default {
  components: { Child },
  data() {
    return {
      tableData: [
        { id: 1, name: '小明' },
        { id: 2, name: '小华' },
        { id: 3, name: '小王' }
      ]
    }
  },
  methods: {
    handleClick(row) {
      console.log({ ...row })
    }
  }
}
</script>
<style lang="less" scoped>
.parent {
  width: 200px;
  border: 1px solid deeppink;
  color: deeppink;
  padding: 10px;
}
</style>

Child.vue

js 复制代码
<template>
  <div class="child">
    <div v-for="row of tableData" :key="row.id">
      <slot :row="row" />
    </div>
  </div>
</template>
<script>
export default {
  props: ['tableData']
}
</script>
<style lang="less" scoped>
.child {
  width: 150px;
  border: 1px solid deepskyblue;
  color: deepskyblue;
}
</style>
相关推荐
奔跑的web.1 天前
TypeScript 装饰器入门核心用法
前端·javascript·vue.js·typescript
集成显卡1 天前
Lucide Icons:一套现代、轻量且可定制的 SVG 图标库
前端·ui·图标库·lucide
pas1361 天前
37-mini-vue 解析插值
前端·javascript·vue.js
十里-1 天前
vue.js 2前端开发的项目通过electron打包成exe
前端·vue.js·electron
雨季6661 天前
构建 OpenHarmony 简易文字行数统计器:用字符串分割实现纯文本结构感知
开发语言·前端·javascript·flutter·ui·dart
小北方城市网1 天前
Redis 分布式锁高可用实现:从原理到生产级落地
java·前端·javascript·spring boot·redis·分布式·wpf
console.log('npc')1 天前
vue2 使用高德接口查询天气
前端·vue.js
2401_892000521 天前
Flutter for OpenHarmony 猫咪管家App实战 - 添加支出实现
前端·javascript·flutter
天马37981 天前
Canvas 倾斜矩形绘制波浪效果
开发语言·前端·javascript
天天向上10241 天前
vue3 实现el-table 部分行不让勾选
前端·javascript·vue.js