vue3 传值方式

一.Props

// 父组件
<div>
    <children :flag="state.eflag" />
</div> 
  
<script setup>
    import children from './children.vue'
    import { reactive } from 'vue'

    const state = reactive({
        eflag: true,
    })
</script>


// 子组件
 <div>
    <div>{{ flag }}</div>
 </div>

<script setup>
    const prop = defineProps({
        flag: {
         type: Boolean, // Object , Array , Function , Number
         default: false
        }
    })
</script>

二.emit

// 子组件
<template>
    <div >
        <el-button type="primary" @click="handleEdit(item)">
            点我
        </el-button>
    </div>
</template>

<script setup>

    // handleEdit子组件方法, selfDataEdit父组件方法
    const emit = defineEmits([
        'selfDataEdit',
    ])
    const handleEdit = (row) => {
        emit('selfDataEdit', row)
    }

    defineExpose({
        handleEdit,
    })
</script>

// 父组件
<template>
    <div>
        <Table @selfDataEdit="handleEdit"></Table>
    </div>
</template>

<script setup>

    import Table from '@/components/common/Table/index.vue'

    const handleEdit = (value) => {
        // 要执行的方法
    }
</script>

三.v-model

// 父组件
<template>
    <child-components v-model:num="state.num"></child-components>
</template>

<script setup>
    import { reactive } from 'vue'
    import ChildComponents from './child.vue'

    const state = reactive({
        num: 721
    })

</script>

//子组件
<template>
    <div>
      <button @click="handleAdd" type="button">添加</button>
    </div>
</template>
<script setup>
    import { defineEmits, defineProps } from 'vue'

    const props = defineProps({
        num: {
            type: Number,
            default: 0,
        },
    })

    const emits = defineEmits(['update:list'])

    const handleAdd = (val) => {
        emits('update:num', val) //update:固定写法
    }
</script>

四.ref

// 父组件
<template>
    <son ref="childMap" @click="showCont" />
</template>
<script setup>
const childMap = ref(null);
const showCont = () => {
   childMap.value.state.num; // 调用子组件的参数
   childMap.value.clearMap("gczLayer"); // 调用子组件的方法
}
<script>

// 子组件
<template></template>
<script setup>
import { reactive } from "vue";

const state = reactive({
    num: 3
})
const clearMap = (val) => {
    console.log('这是子组件', val)
}

defineExpose(
  state,
  clearMap
)
<script>

五. provide/inject

如果父组件使用readonly  这么设置 provide('numVal', readonly (num)),那么子孙及组件不可更改该值

//父组件
<template>
  <div>
    <p>父:{{ num }}</p>
    <button @click="dianwo">父级点击</button>
    <left></left>
  </div>
</template>
  
<script setup>
import { ref, provide } from "vue";
import left from "./left.vue";


const num = ref(0)

provide('numVal', num)

const dianwo = () => {
  num.value = 1
};
</script>


//子组件
<template>
  <!-- <p>子:{{num}}</p> -->
  <!-- <button @click="dianwo">子级点击</button> -->
  <components></components>
</template>
    
<script setup>
import { ref, reactive, inject } from "vue";
import components from "./virtually/components.vue";

// let num = inject('numVal')

const dianwo = () => {
  console.log('asd', num)
  num.value = 2
}
</script>


//孙子组件
<template>
  <p>孙子:{{num}}</p>
  <button @click="dianwo">孙子级点击</button>
</template>
    
<script setup>
import { ref, reactive, inject } from "vue";

let num = inject('numVal')

const dianwo = () => {
  console.log('asd', num)
  num.value = 3
}
</script>

六.其余

当然还有 pinia 本地存储 混入之类的传值,跟vue2差不多,就不一一解释了

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晚风吹心事

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值