Vue3父子组件传值

父组件

<!--
 * @Author: wangyf 1758985226@qq.com
 * @Date: 2023-07-03 15:33:36
 * @LastEditors: wangyf 1758985226@qq.com
 * @LastEditTime: 2023-07-03 16:32:00
 * @FilePath: \fast-vue3\src\pages\fcvalue\index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div>
    <h1>我是父组件</h1>
    <br />
    <span>子组件传来的值=====》 {{ ziValue }}</span>
    <br />
    <span>---------------------------------------</span>
    <children ref="childrenref" :msg="msg" @getChildren="getChildren" />
    <span>---------------------------------------</span>
    <br />
    <button @click="changeChilren">父组件修改子组件的值</button>
  </div>
</template>

<script setup>
  import { ref } from 'vue';
  import children from './children.vue';
  const msg = ref('我是父组件的值');
  const ziValue = ref('');
  // 父组件调用子组件函数
  const childrenref = ref(null);
  const changeChilren = () => {
    childrenref.value.changeed();
  };
  //子组件调用父组件函数
  const getChildren = (value) => {
    ziValue.value = value;
    console.log(value);
  };
</script>

<style></style>

子组件

<template>
  <div>
    <h1>我是子组件</h1><br />
    <h1> {{ chlidrenValue }}</h1>
    <h1>我是父组件传来的值===》 {{ msg }}</h1>
    <span>我是aaa:{{ aaa }}</span> <br />
    <button @click="changeParent"> 子组件传父组件值</button>
  </div>
</template>

<script setup>
  import { defineProps, ref } from 'vue';
  defineProps({
    msg: {
      type: String,
      default: 'oooo',
    },
  });
  const chlidrenValue = ref('子组件的值');
  const aaa = ref('我是子组件的AAA');
  const emit = defineEmits(['getChildren']);
  const changeParent = () => {
    emit('getChildren', '20000');
    console.log('子组件');
  };
  const changeed = () => {
    aaa.value = '我被父组件改变了';
  };
  defineExpose({ aaa, changeed });
</script>

<style></style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值