javascript - 未使用 VueJS mixins 定义属性

标签 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 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.

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

最佳答案

您必须将 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.js';
export default {
    mixins: [ErrorBaseMixin],
...

注意:注意如何导入和导出,我已经改变了方式。

关于javascript - 未使用 VueJS mixins 定义属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54025845/

相关文章:

javascript - 如何通过更改 css 高度和宽度来制作小加载圆?

javascript - 如何禁用 Google 表单 iframe 的默认浏览器消息 “are you sure you want to leave this page”?

javascript - 向 vue 组件添加属性

javascript - vuejs单独推送标签到input

javascript - 将 iframe 的 src =""更改为 javascript :

javascript - 如何在 Vue 组件中使导出的变量具有反应性?

vue.js - 在 Vuetify 数据表上显示 bool 值

javascript - 在哪里/如何为 Vue Js 方法定义数据变量?

vue.js - Vuetify 转换 : How to set the transition speed

javascript - 为什么返回default case而不是扔进Redux的reducer?