Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
468 views
in Technique[技术] by (71.8m points)

laravel - How to pass an object from axios catch block to parent component with Vue.js

I am using Laravel 7 and Vue.js 2.

I want to pass an object of validation errors from the catch block of an axios call to a parent component but for some reasons it doesn't work.

This is the code of the axios call:

        runReport: function() {
            let self = this;
            const url = "api/get_report?room="+this.formReport['room']+"&participant="+this.formReport['participant']+"&start="+this.formReport['start']+"&end="+this.formReport['end'];
            axios.get(url)
            .then((response) => {
                console.log(response.data.data);
                this.meetingsReport = response.data.data;
                this.$emit('passMeetings', this.meetingsReport);
                this.$emit('success');
                this.errors = {};
            })
            .catch(function(error) {
                console.log(error.response.data);
                self.errors = error.response.data;
                alert(self.errors);
                self.$emit('failure');
                self.$emit('passErrors', self.errors); //problem
                console.log('call ended');
            });
        }

This is the code in the parent component:

<template>
    <div>
        <report-meeting @passMeetings="onPassMeetings" @failure="displayTable=false" @success="displayTable=true"></report-meeting>
        <hr>
        <validated-errors :errorsMeeting="errorsMeeting" @passErrors="onPassErrors" v-if="displayTable===false"></validated-errors>
        <table-report :meetingsSelected="meetingsSelected" v-if="displayTable===true"></table-report>
    </div>
</template>

<script>
    import TableReport from "./TableReport.vue"
    import ReportMeeting from "./ReportMeeting.vue"
    import ValidatedErrors from "./ValidatedErrors.vue"

    export default {
        components: {
            'table-report': TableReport,
            'report-meeting': ReportMeeting,
            'validated-errors': ValidatedErrors
        },
        mounted() {
            console.log('Component mounted.');
        },
        data: function() {
            return {
                displayTable: false,
                meetingsSelected: {},
                errorsMeeting: {}
            }
        },
        methods: {
            onPassMeetings(value) {
                console.log(value);
                this.meetingsSelected = value;
            },
            onPassErrors(value) {
                console.log('errors passed'); //never used
                this.errorsMeeting = value;
            }
        }
    }
</script>

In the console I visualize no errors (except an 422 Unprocessable Entity). The strange thing is that the first emit works (failure), but the second one doesn't work (passErrors). In the parent function onPassErrors I put a console.log that is never used so I suppose that the function is never called. Can help?

question from:https://stackoverflow.com/questions/65541113/how-to-pass-an-object-from-axios-catch-block-to-parent-component-with-vue-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is likely caused by an event name mismatch, which can occur when using in-DOM templates because HTML attributes are automatically lower-cased (@passErrors becomes @passerrors in the DOM).

When using the development build of Vue, you'd see a warning in the browser's console:

[Vue tip]: Event "passerrors" is emitted in component but the handler is registered for "passErrors". Note that HTML attributes are case-insensitive and you cannot use v-on to listen to camelCase events when using in-DOM templates. You should probably use "pass-errors" instead of "passErrors".

This is not a problem in single file components (demo 1) or string templates (demo 2), but if you must stick with in-DOM templates, custom event names are recommended to be kebab-case:

<!-- Parent.vue -->
<MyComponent @pass-errors="onPassEvent" />
// MyComponent.vue
runReport() {
  this.$emit('pass-errors', /*...*/)
}

demo 3


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...