2
votes

I try to use Mixins with Vue.js. But I encounter several issues with them :/

This is my current code for my two test modules :

ErrorBaseMixin.vue

<script>
    import ErrorAlert from './ErrorAlert';

    export const ErrorBaseMixin = {
        data() {
            return {
                // Errors management
                error_display: true,
                error_data: {
                    level: "warning",
                    time: 0,

                    status: 200,
                    message: ""
                }
            }
        },
        methods: {
            // ------------------------------------------------------------------------
            // Errors management functions
            // ------------------------------------------------------------------------
            error_function_show_error: function() {
                try {
                    this.$refs.error_component.launch();
                }
                catch {}
            },

            callback_error_catched: function(e) {
                if(e.message === 'Network Error'){
                    this.error_data.message = "<strong>There was a network error :</strong> The connection is broken or the server is not started.";
                    this.error_data.level = "danger";
                }
                else {
                    this.error_data.message = "An error occured : " + e.message;
                    this.error_data.level = "warning";
                }

                this.error_function_show_error();
            },
        },
        components: {
            ErrorAlert
        }
    }

    export default ErrorBaseMixin;
</script>

Test.vue

<template>
        <ErrorAlert
            :error_display="error_display"
            :error="error_data"
            ref="error_component"
        />
    </div>
</template>

<script lang="js">
    import {ErrorBaseMixin} from '../../../parts/ErrorBaseMixin.vue';

    export default {
        mixins: [ErrorBaseMixin],
        name: 'Test_elt',
        created() {
            this.REST_ADDR = "test/test";
        },
        data() {
            return {
                field: {
                    id: '55',
                    name: 'test'
                }
            }
        },
        methods: {

        }
    }
</script>

But when I compile the last module, I have the following errors in my browser console :

[Vue warn]: Property or method "error_data" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option or for class-based components, by initializing the property.

[Vue warn]: Unknown custom element: - did you register the component correctly? For recursive components, make sure to provide the "name" option.

But... Everything is working fine. So I don't understand why I have these errors

1
Can you try setting a name property in ErrorBaseMixin.vue? That may rule out warnings altogether.GMaiolo
Mixins should just be a js file. So just change the filename from ErrorBaseMixin.vue to ErrorBaseMixin.js and remove the surrounding <script> tags.thanksd
First you have export const ErrorBaseMixin and then export default ErrorBaseMixin;Adam Orlov

1 Answers

4
votes

You must change ErrorBaseMixin.vue to ErrorBaseMixin.js:

import ErrorAlert from './ErrorAlert';

const ErrorBaseMixin = {
    data() {
        return {
            // Errors management
            error_display: true,
            error_data: {
                level: "warning",
                time: 0,

                status: 200,
                message: ""
            }
        }
    },
    methods: {
        // ------------------------------------------------------------------------
        // Errors management functions
        // ------------------------------------------------------------------------
        error_function_show_error: function() {
            try {
                this.$refs.error_component.launch();
            }
            catch {}
        },

        callback_error_catched: function(e) {
            if(e.message === 'Network Error'){
                this.error_data.message = "<strong>There was a network error :</strong> The connection is broken or the server is not started.";
                this.error_data.level = "danger";
            }
            else {
                this.error_data.message = "An error occured : " + e.message;
                this.error_data.level = "warning";
            }

            this.error_function_show_error();
        },
    },
    components: {
        ErrorAlert
    }
}

export default ErrorBaseMixin;

And then import in your component:

import {ErrorBaseMixin} from '../../../parts/ErrorBaseMixin.js';
export default {
    mixins: [ErrorBaseMixin],
...

Note: Take care how import and export, I have changed the way.