• 3.vue3.2的父传子defineProps,子传父emits以及ref


    1.父传子

    <template>
      <div class="container">
         
        <Hello title="我是hello的爸爸" :list='list'/>
        <hr>
        <h4>子组件传数据过来了 {{fromSon}}h4>
      div>
    template>
    
    <script setup>
    import { reactive, toRefs } from 'vue'
    //导入子组件
    import Hello from '@/components/HelloWorld'
    const list = reactive([
      { id: 1, name: '哈哈哈' },
      { id: 2, name: '嘿嘿嘿' },
      { id: 3, name: '呵呵呵' },
    ])
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    子接收
    <template>
      <div class="container">
        我是Hello
        <h5>父组件传了一句话过来 String---- {{title}}h5>
        <h5>父组件传了一个数组过来 Array --- {{list}}h5>
      div>
    template>
    <script setup>
    import { reactive, toRefs } from 'vue'
        // 通过defineProps接收,之后直接声明
        defineProps({
          title:String,
          list:Array
        })
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2.子传父 emits传递

    <template>
      <div class="container">
        <button @click="clickTap">点击这里给父组件传递些许数据button>
      div>
    template>
    <script setup>
    import { reactive, toRefs } from 'vue'
    
    // 这里可以传递多个值
      const list = reactive([1,2,3,4])
      // defineEmits内跟一个数组,数据内写绑定的事件
      const emit = defineEmits(['on-click'])
      //进行数据传递
      const clickTap = () => {
        emit('on-click',list,true)
      }
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    父接收
    <template>
      <div class="container">
        
        <Hello @on-click="getList" />
        <hr>
        <h4>子组件传数据过来了 {{fromSon}}h4>
      div>
    template>
    
    <script setup>
    import { reactive, toRefs } from 'vue'
    import Hello from '@/components/HelloWorld'
    
    const fromSon = reactive([])
    const getList = (list,flag) => {
      // 这里不能直接复制,会破坏双休绑定数据
      fromSon.push(...list) 
      console.log(flag);
      console.log('子组件传过来的值',list);
    }
    script>
    
    <style lang="scss" scoped>
        
    style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    3.子传父 通过ref传递
    <template>
      <div class="container">
        <button @click="clickTap">点击这里给父组件传递些许数据button>
      div>
    template>
    
    <script setup>
    import { reactive, toRefs } from 'vue'
    // 这里可以传递多个值
    const list = reactive([1,2,3,4])
    // defineEmits内跟一个数组,数据内写绑定的事件
    const emit = defineEmits(['on-click'])
    const clickTap = () => {
      emit('on-click')
    }
    // 暴露出list
    defineExpose({
      list
    })
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    父接收
    <template>
      <div class="container">
        
        <Hello ref="menus" @on-click="getList" />
      div>
    template>
    
    <script setup>
    import { reactive, toRefs,ref } from 'vue'
    import Hello from '@/components/HelloWorld'
    
    // 还可以通过ref进行字传父
    const menus = ref(null)    
        
    const getList = (list,flag) => {
      console.log(menus.value);
    }
    script>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
  • 相关阅读:
    【04】MapReduce
    浅谈web性能测试
    任务调度器详解(FreeRTOS)
    YOLOv5项目实战(3)— 如何批量命名数据集中的图片
    Asante Capital Group加强在亚洲的战略重点
    【圆环展开算法剖析与硬核实战--全程高能,做好笔记】
    SAP SALV14 增强SALV使SALV支持列级别、行级别、单元格级别的编辑模式切换
    javascript输出数据在控制台中console的常用方法(上)
    MyBatisPlus学习(4)—— 设置实体类
    图像的频域--学习笔记
  • 原文地址:https://blog.csdn.net/m0_56986233/article/details/125853414