VUE3 学习笔记(9):VUE 插槽的概念、基本应用、传值

在调用子组件时,我们希望把父组件的HTML传给子组件,那么在引用子组件内部进行定义,然后子组件通过slot标签进行接收

基本示例

父 app.vue

html 复制代码
<!--内容控制-->
<template>

  <test>
    <div>
      <p>{{name}}</p>
      <p>{{age}}</p>
    </div>
  </test>

</template>
<!--JS 控制-->
<script>

import test from "@/components/test.vue";


export default {
  components: {test},
  data() {
    return {
      name: '李四',
      age: 100
    }
  }

}
</script>

子 test.vue

html 复制代码
<template>
  <div>
    <p>测试插槽:</p>
    <slot></slot>
  </div>
</template>
<script setup lang="ts">
</script>

自定义插槽

有时我们并不需要把相关需要的信息全部传过去或者需要根据条件进行局部显示,那么需要通过<template v-slot:命名>(也可以#命名)定义插槽进行命名,然后插槽时指定<slot name="命名"></slot>来实现。

父 app.vue

html 复制代码
<!--内容控制-->
<template>

  <test>
    <template v-slot:ProName>
      <div>
        <p>{{name}}</p>
      </div>

    </template>
    <!-- 也可以如下命名   -->
    <template #ProAge>
      <div>
        <p>{{age}}</p>
      </div>

    </template>

  </test>

</template>
<!--JS 控制-->
<script>

import test from "@/components/test.vue";


export default {
  components: {test},
  data() {
    return {
      name: '李四',
      age: 100
    }
  }

}
</script>

子 test.vue

html 复制代码
<template>
  <div>
    <p>测试插槽:</p>
    <slot name="ProName"></slot>
    <br>
    <slot name="ProAge"></slot>
  </div>
</template>

子组件反向对父组件进行传值

有时我们会需要把子组件的数据要传回给父组件(反向传值),子组件绑定并值并命名,

父组件通过v-slot="命名"的接收。

子 test.vue

html 复制代码
<template>
  <div>
    <p>测试插槽:</p>
    <slot name="ProName"></slot>
    <br>
    <slot name="ProAge"></slot>
    <br>

    <slot :msg="sex"></slot>
    <slot name="ProBthDay" :BthDay="BthDay"></slot>
  </div>
</template>
<script >
export default {
  data() {
    return {
      sex: '男',
      BthDay: '1999-01-01'
    }
  }
}
</script>

父 app.vue

html 复制代码
<!--内容控制-->
<template>

  <test>
    <template v-slot:ProName>
      <div>
        <p>{{name}}</p>
      </div>

    </template>
    <!-- 也可以如下命名   -->
    <template #ProAge>
      <div>
        <p>{{age}}</p>
      </div>
    </template>

    <template v-slot="ProSex" >
      <div>
        <p>接收到:{{ProSex.msg}}</p>
      </div>
    </template>

    <template #ProBthDay ="ProBthDay" >
      <div>
        <p>接收到:{{ProBthDay.BthDay}}</p>
      </div>
    </template>


  </test>

</template>
<!--JS 控制-->
<script>

import test from "@/components/test.vue";


export default {
  components: {test},
  data() {
    return {
      name: '李四',
      age: 100
    }
  }

}
</script>
相关推荐
网络点点滴16 分钟前
Vue3的生命周期
前端·javascript·vue.js
梵得儿SHI33 分钟前
Vue 核心语法之组件基础与通信:从创建到注册的完整指南
前端·javascript·vue.js·组件化开发·全局注册·vue组件的本质·局部注册和异步组件
一颗烂土豆2 小时前
告别 Vue 多分辨率适配烦恼:vfit 让元素定位 “丝滑” 跨设备
前端·vue.js
萌狼蓝天2 小时前
[Vue]性能优化:动态首行与动态列的匹配,表格数据格式处理性能优化
前端·javascript·vue.js·性能优化·ecmascript
一 乐2 小时前
宠物管理宠物医院管理|基于Java+vue的宠物医院管理系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·宠物
一 乐2 小时前
学习辅导系统|数学辅导小程序|基于java+小程序的数学辅导小程序设计与实现(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·学习·小程序
2503_928411563 小时前
11.20 vue项目搭建-单页面应用
前端·javascript·vue.js
苏小画3 小时前
Vue 组件库从创建到发布全流程
前端·javascript·vue.js