【问题标题】:VueJS - Run Code after event is successfully emittedVueJS - 成功发出事件后运行代码
【发布时间】:2021-04-01 19:29:07
【问题描述】:

在包含输入表单数据的事件成功从子组件传递到父组件后,我正在尝试清除子组件中的表单。但是,我注意到表单在数据通过事件传播到父组件之前被清除,因此事件将空值传递给父组件。我尝试使用超时来延迟 clearForm() ,但它没有帮助。有没有办法修改行为,使 clearForm() 仅在事件完成并保存数据后发生?

附上代码。

子组件

<template>
    <!-- Contains a form -- >
</template>

<script>
export default {
    
    data() {
        return {
            additionalInfo: 
                {
                    id: new Date().toISOString(),
                    fullName: '',
                    preAuthorize: '',
                    serviceAddress: ''
                },
            validation: {
                fullNameIsValid: true,
                serviceAddressIsValid: true
            },
            formIsValid: true,
            addServiceButtonText: '+ Add Service Notes (Optional)',
            serviceNotes: [],
            showServiceNotes: false,
            enteredServiceNote: '', //service notes addendum
        }
    },
    computed : {
        // something
    },
    methods: {
        
        setServiceNotes(){
            this.showServiceNotes = !this.showServiceNotes;
        },
        addAnotherParty(){
            this.validateForm();
            if(!this.formIsValid){
                return;
            }
            this.$emit('add-parties', this.additionalInfo); //event
            console.log(this.clearForm);
        },
        
        clearForm(){
            this.additionalInfo.fullName = '';
            this.additionalInfo.serviceAddress = '';
            this.additionalInfo.preAuthorize = false;
        }
    }
}
</script>

父组件

<template>
    <div>
        <base-card 
        ref="childComponent"
        @add-parties="updateAdditionalInfoList">
            <!-- Wrapper for the `Parties Being Served` component-->
                <template v-slot:title>
                    <slot></slot>
                </template>

        </base-card>
    </div>
    
</template>
<script>
export default {
    data() {
        return {
            hasElement: false,
            selectedComponent: 'base-card',
            additionalInfoList : [],
            clearForm: false
        }
    },
    methods: {
        updateAdditionalInfoList(additionalInfo){ //save changes passed via event
            this.additionalInfoList.push(additionalInfo);
            console.log('emitted');
            console.log(this.additionalInfoList);    
            setTimeout(() => {
                this.$refs.childComponent.clearForm();  //clear the form in child
            }, 2000);
            
        }
    }
}
</script>

【问题讨论】:

    标签: javascript vue.js events vuejs2 eventemitter


    【解决方案1】:

    试试这个

    addAnotherParty(){
      this.validateForm();
      if(!this.formIsValid){
        return;
      }
      let emitObj = JSON.parse(JSON.stringify(this.additionalInfo));
      this.$emit('add-parties', emitObj); //event
      console.log(this.clearForm);
    }
    

    如果你的对象不深,那么你可以使用

    let emitObj = Object.assign({}, this.additionalInfo);
    

    而不是字符串化和解析

    【讨论】:

    • 谢谢。它现在就像一个魅力。介意我问一下 JSON 的使用如何导致事件在表单清除之前完成?
    • 你不能在 JS 中没有引用的情况下分配对象。因此,无论您在分配的对象中进行了什么更改,它都会反映到您用于分配的变量中。 Object.assign 将为第一个层次结构创建一个带有新引用的浅拷贝。但是 JSON.stringify 将创建一个全新的引用,因为您正在转换为字符串.. 并解析另一个引用,但您将获得另一个引用,但作为对象。同样适用于 JS 中的数组
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 2020-11-25
    • 2017-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多