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>
相关推荐
工业互联网专业2 小时前
基于springboot+vue的秦皇岛旅游景点管理系统
java·vue.js·spring boot·毕业设计·源码·课程设计·旅游景点管理系统
枫super4 小时前
Day-03 前端 Web-Vue & Axios 基础
前端·javascript·vue.js
计算机学姐4 小时前
基于SSM的校园美食交流系统
java·vue.js·mysql·spring·tomcat·mybatis·美食
程序猿chen4 小时前
Vue.js组件安全工程化演进:从防御体系构建到安全性能融合
前端·vue.js·安全·面试·前端框架·跳槽·安全架构
huige99995 小时前
Vue 与 ORM 的对比理解:抽象的力量
vue.js·orm
爱的叹息5 小时前
容器初始化Spring Boot项目原理,即web项目(war)包涉及相关类对比详解
前端·vue.js·typescript
pink大呲花5 小时前
Vue.js 中 v-if 的使用及其原理
前端·javascript·vue.js
天官赐福_6 小时前
vue2的scale方式适配大屏
前端·vue.js
季禮祥6 小时前
Vite+Vue报错TypeError: Failed to fetch dynamically imported module,阁下该如何应对?
vue.js·vite
skyWang4166 小时前
基于 Vue3 和 Tiptap的在线多人协同编辑文本编辑器
vue.js