vue3.0版本中怎么使用emit向父组件传值(TS)

因为需要用到TS,所以使用了VUE3.0的版本
页面效果在这里插入图片描述

下面是新版本中父子组件传值用法

父组件中

首先:父组件引入子组件并传值,绑定方法
完整代码

<template>
  <div class="app">
    <ButtonToolbar :values="value" @toolabClick="toolabFun" :childFun="parentFun"/>
  </div>
</template>

<script lang="ts">
import { defineComponent, defineAsyncComponent, reactive, toRefs } from "vue";
import ButtonToolbar from "@/components/btnToolbar/index.vue";

export default defineComponent({
  name: "Main",
  components: {
    ButtonToolbar,
  },
  setup() {
    const data = reactive({
      value: ["新增", "编辑", "删除", "保存", "流程图"],
      // 第一种方法
      toolabFun(key: Number) {
        console.log(key);
      },
      // 第二种方法
      parentFun(key: Number) {
        console.log(key);
      },
    });
    return {
      ...toRefs(data),
    };
  },
});
</script>

<style lang="scss" scoped></style>

子组件

接受值

  props: {
    values: {
      type: Array,
      default: () => [],
    },
    childFun: {
      type: Function,
      required: true,
    },
  },

匹配值并绑定事件并触发父事件

 setup(props, context) {
    const data = reactive({
      buttonArray: [
        { id: 1, type: "primary", name: "新增", fun: "add" },
        { id: 2, type: "primary", name: "编辑", fun: "edit" },
        { id: 3, type: "primary", name: "保存", fun: "save" },
        { id: 4, type: "danger", name: "删除", fun: "delete" },
        { id: 5, type: "primary", name: "刷新", fun: "refresh" },
        { id: 6, type: "primary", name: "流程图", fun: "chart" },
      ],
      buttonArrayTem: [{}],

      init() {
        console.log(props.values)
      },

      clickFunction(key: Number) {
        // 第一种方法
        console.log(context);
        context.emit("toolabClick", key);
        // 第二种方法
        props.toolabClick(key);
      },
    });
    onMounted(() => {
      data.init();
    });

    return {
      ...toRefs(data),
    };
  },
**第一种的方法重点就在于在setup中传的参数content,打印一下**
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210114112559363.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NTTkQ3OTk3,size_16,color_FFFFFF,t_70)
**通过content.emit来触发父组件**
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3,子组件可以使用`$emit`方法向其父组件派发事件,而父组件可以监听该事件并处理数据。如果你想要将数据传递给父组件的父组件,可以使用`$emit`方法将事件派发到父组件,并让父组件再将该事件向上派发,以便其父组件监听到该事件。 以下是一个示例: 假设有一个父组件`Grandparent`,一个子组件`Parent`和一个孙子组件`Child`。我们希望在`Child`组件触发一个事件,并将数据传递到`Grandparent`组件。 在`Child`组件,可以使用`$emit`方法将事件派发到其父组件`Parent`: ```javascript // Child.vue <template> <button @click="triggerEvent">触发事件</button> </template> <script> import { defineComponent } from 'vue' export default defineComponent({ methods: { triggerEvent() { this.$emit('my-event', '传递的数据') } } }) </script> ``` 在`Parent`组件,监听`Child`组件派发的事件,并将该事件向上派发: ```javascript // Parent.vue <template> <Child @my-event="handleEvent"></Child> </template> <script> import { defineComponent } from 'vue' export default defineComponent({ methods: { handleEvent(data) { this.$emit('my-event', data) } } }) </script> ``` 在`Grandparent`组件,监听`Parent`组件派发的事件: ```javascript // Grandparent.vue <template> <Parent @my-event="handleEvent"></Parent> </template> <script> import { defineComponent } from 'vue' export default defineComponent({ methods: { handleEvent(data) { console.log(data) // 输出:传递的数据 } } }) </script> ``` 这样就可以在`Child`组件触发一个事件,并将数据传递到`Grandparent`组件了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值