vue 组件 子组件向父组件传参 自定义事件 $emit

需求:列表页的内容项可添加  ,点击“自定义”弹出框   添加才做完成后  刷新列表

效果图:

父组件

子组件(弹出框)

 

思路:

1、在子组件完成添加操作后调用emit触发绑定函数,

2、在父组件里自定义事件名,并进行刷新数据的逻辑处理

 

代码  Addstation.vue(父组件)   父组件里自定义事件名   @afterAdd="updateList"

<addwelfare ref="welfareMsg"
                    @afterAdd="updateList"
                    :companyId="firmid"></addwelfare>
  //子组件传过来的
        updateList(data) {
            //添加完成的事件
            console.log("子组件");
            console.log(data);   //"已添加"  子组件
            this.welfareLabelListadd = [];
            this.welfarelabel();
        },

Addwelfare.vue(子组件)

子组件里自定义事件   this.$emit("afterAdd", "已添加");  调用emit触发绑定函数

  addwelfare() {
            // let vm = this;
            this.$refs.welfareForm.validate((isvalid) => {
                if (isvalid) {
                    this.$request
                        .addwelfareTag({
                            companyId: Number(this.companyId),
                            label: this.ruleForm.welfareItem,
                        })
                        .then((res) => {
                            // console.log(res); //福利标签
                            if (res.code == 200) {
                                this.$message({
                                    message: "添加成功",
                                    type: "success",
                                });
                                this.$emit("afterAdd", "已添加");
                                this.dialogVisibl = false;
                            } else if (res.code == 600) {
                                this.$message({
                                    message: "企业会员信息有误",
                                    type: "warning",
                                });
                            } else {
                                this.$message({
                                    message: "添加失败",
                                    type: "warning",
                                });
                            }
                        });
                }
            });
        },

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Vue 中,组件向父组件传参可以通过自定义事件的方式实现。具体步骤如下: 1. 在组件中定义一个方法,该方法用于触发自定义事件并传递参数。 2. 在组件中使用 `$emit` 方法触发自定义事件,该方法接收两个参数,第一个参数是自定义事件的名称,第二个参数是要传递的参数。 3. 在父组件中通过 `v-on` 指令监听自定义事件,该指令可以缩写为 `@`。 4. 在父组件的方法中,可以通过 `$event` 参数来获取组件传递过来的参数。 下面是一个示例代码: 组件: ``` <template> <button @click="sendData">传递数据</button> </template> <script> export default { methods: { sendData() { const data = 'hello world'; this.$emit('send-data', data); } } } </script> ``` 父组件: ``` <template> <div> <child-component @send-data="handleData"></child-component> </div> </template> <script> export default { methods: { handleData(data) { console.log(data); // 输出:hello world } } } </script> ``` 在上面的代码中,组件中定义了一个 `sendData` 方法,该方法通过 `$emit` 方法触发了一个名为 `send-data` 的自定义事件,并传递了一个字符串参数。 在父组件中,使用 `v-on` 指令监听了组件触发的 `send-data` 事件,并调用了 `handleData` 方法来处理传递过来的参数。在 `handleData` 方法中,可以通过 `$event` 参数来获取组件传递过来的参数。 ### 回答2: Vue 组件向父组件传参的方法有多种。 1. 使用 `$emit` 方法:组件通过 `$emit` 触发自定义事件,并传递参数给父组件,父组件在模板中通过 `v-on` 接收传递的参数。具体步骤如下: - 在组件中定义一个方法,使用 `$emit` 触发自定义事件,并传递参数。例如:`this.$emit('customEvent', yourData)` - 在父组件的模板中,使用 `v-on` 监听组件传递的自定义事件,并接收参数。例如:`<child-component v-on:customEvent="yourMethod"></child-component>` 2. 使用 `.sync` 修饰符: `.sync` 修饰符可以简化组件向父组件传递参数的过程。具体步骤如下: - 在父组件中使用 `<child-component :yourProp.sync="yourData"></child-component>`,其中 `yourProp` 是父组件的接收参数,`yourData` 是组件中数据的名称。 - 在组件中使用 `this.$emit('update:yourProp', newValue)` 更新父组件的数据。 3. 使用 `.sync` 修饰符的替代方法: 在父组件中使用 `:yourProp="yourData" @update:yourProp="yourData = $event"`,其中 `yourProp` 是父组件的接收参数,`yourData` 是组件中数据的名称。 总结:Vue 组件向父组件传参的常见方法有使用 `$emit` 方法触发自定义事件,使用 `.sync` 修饰符以及使用 `.sync` 修饰符的替代方法。根据具体的使用场景,选择合适的方法进行传参。 ### 回答3: 在Vue中,组件向父组件传参有两种常用的方式:使用props和使用自定义事件。下面我将分别介绍这两种方式。 1. 使用props: 在父组件中,可以通过在组件上定义props来接收组件传递的参数。在组件中,通过在标签上使用v-bind指令将需要传递的参数绑定到父组件的数据上,然后在父组件中,可以通过props选项来接收组件传递的参数。 例如,在组件中: ``` <template> <div> <button @click="sendData">传递参数</button> </div> </template> <script> export default { methods: { sendData() { this.$emit('child-event', 'hello') } } } </script> ``` 在父组件中: ``` <template> <div> <child-component @child-event="receiveData"></child-component> </div> </template> <script> export default { methods: { receiveData(data) { console.log(data) // 输出 hello } } } </script> ``` 2. 使用自定义事件: 在组件中,可以通过在组件的方法中使用$emit方法触发自定义事件,并将需要传递的参数作为参数传递给自定义事件。在父组件中,通过在组件标签上使用@监听自定义事件,并在父组件的方法中接收传递的参数。 例如,在组件中: ``` <template> <div> <button @click="sendData">传递参数</button> </div> </template> <script> export default { methods: { sendData() { this.$emit('child-event', 'hello') } } } </script> ``` 在父组件中: ``` <template> <div> <child-component @child-event="receiveData"></child-component> </div> </template> <script> export default { methods: { receiveData(data) { console.log(data) // 输出 hello } } } </script> ``` 以上是组件向父组件传参的两种常用方式,根据实际需要选择适合的方式即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值