未使用VueJS mixins定义的属性

时间:2019-01-03 16:02:54

标签: javascript vue.js vuejs2

我尝试将Mixins与Vue.js结合使用。但是我遇到了一些问题:/

这是我两个测试模块的当前代码:

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>

但是当我编译最后一个模块时,我的浏览器控制台出现以下错误:

  

[Vue警告]:属性或方法“ error_data”未在   实例,但在渲染过程中被引用。确保此属性是   在data选项或基于类的组件中,通过   初始化属性。

     

[Vue警告]:未知的自定义元素:-您是否已注册   组件正确吗?对于递归组件,请确保   提供“名称”选项。

但是...一切正常。所以我不明白为什么会有这些错误

1 个答案:

答案 0 :(得分:1)

您必须将ErrorBaseMixin.vue更改为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;

然后导入您的组件:

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

注意:请注意,进出口方式已更改,