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>
相关推荐
sunbyte4 分钟前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | GoodCheapFast(Good - Cheap - Fast三选二开关)
前端·javascript·css·vue.js·tailwindcss
拼图20911 小时前
element-plus——图标推荐
javascript·vue.js·elementui
midsummer_woo14 小时前
基于springboot+vue+mysql工程教育认证的计算机课程管理平台(源码+论文)
vue.js·spring boot·mysql
喝拿铁写前端15 小时前
技术是决策与代价的平衡 —— 超大系统从 Vue 2 向 Vue 3 演进的思考
前端·vue.js·架构
豆豆(设计前端)15 小时前
如何成为高级前端开发者:系统化成长路径。
前端·javascript·vue.js·面试·electron
苦瓜若叶睦15 小时前
Vue (Official) v3.0.2 新特性 为非类npm环境引入 globalTypesPath 选项
vue.js
springfe010116 小时前
模块与组件区别
javascript·vue.js
结衣结衣.17 小时前
Vue3入门-计算属性+监听器
前端·javascript·vue.js·vue·js
哎呦薇18 小时前
从开发到发布:手把手教你将Vue组件上传npm
前端·vue.js
用户38022585982418 小时前
vue3源码解析:生命周期
前端·vue.js·源码阅读