vue3.0 02 父组件中创建自定义事件,使用emits动态获取子组件中的数据变化 实现子组件向父组件的通信

1. 场景模拟

父子组件通信

子组件中的 add() 事件执行的时候, 使用emit调用父组件中的自定义事件,实现一次子向父组件的通信

2. 父组件页面

[1] TestEmit 为子组件,在调用过程中创建changeZidingyi() 事件,在子组件调用该方法的时候, 修改父组件中的数据

<TestEmit @changeZidingyi="sonChange" />

[2] 定义父组件中的响应数据和自定义方法,使用自定义方法中的参数获取子组件传递过来的数据

  setup() {
    let emitNum = ref(0);
    // 创建一个数据跟随子组件数据变动的方法,供子组件调用
    let sonChange = (v) => {
      emitNum.value = v;
    };

    return { emitNum, sonChange };
  },

[3] 父组件页面源码如下:

<template>
  <!-- <HelloWorld msg="Welcome to Your Vue.js App" /> -->
  <TestEmit @changeZidingyi="sonChange" />
  测试子组件向父组件通信: {{ emitNum }}
</template>

<script>
import HelloWorld from "./components/HelloWorld.vue";
import TestEmit from "./components/testEmit.vue";
import { ref } from "vue";
export default {
  name: "App",
  components: {
    HelloWorld,
    TestEmit,
  },
  setup() {
    let emitNum = ref(0);
    // 创建一个数据跟随子组件数据变动的方法,供子组件调用
    let sonChange = (v) => {
      emitNum.value = v;
    };

    return { emitNum, sonChange };
  },
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

3.子组件页面 重点在于setup中传递的第二个参数context

[1] 使用emits 声明父组件中的自定义事件

  emits: ["changeZidingyi"],

[2] 在子页面中使用context获取到emit中的数据 注意,context位于第二个位置,第一个位置的返回值是父传子用到的props

  setup(props, context) {
    console.log("页面中输出的数据是:", context);
    const { emit } = context;
  },

[3] 在add方法中调用emit中定义的父组件的自定义方法,并且将num的数据传递给父组件

    let num = ref(5);

    let add = () => {
      num.value++;
      emit("changeZidingyi", num.value);
    };

    return { num, add };

[4] 使用watchEffect让子组件在一开始加载的时候,就调用父组件的自定义方法,将初始值传递给父组件,保持页面数据的一致性

    // 监听num数据的变化情况
    watchEffect(() => {
      //   初始化的时候调用,保持两边数据一致
      if (num.value < 0) {
        num.value = 0;
      }
      emit("changeZidingyi", num.value);
    });

[5] 子组件全部代码如下

<template>
  <dev>
    <h1>子组件数据改变父组件直接获取改变</h1>
    <button @click="add()">+</button>{{ num }}
  </dev>
</template>

<script>
import { ref, watchEffect } from "vue";
export default {
  emits: ["changeZidingyi"],

  setup(props, context) {
    console.log("页面中输出的数据是:", context);
    const { emit } = context;

    let num = ref(5);

    let add = () => {
      num.value++;
      emit("changeZidingyi", num.value);
    };

    // 监听num数据的变化情况
    watchEffect(() => {
      //   初始化的时候调用,保持两边数据一致
      if (num.value < 0) {
        num.value = 0;
      }
      emit("changeZidingyi", num.value);
    });

    return { num, add };
  },
};
</script>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值