未知的自定义元素和指令

时间:2018-12-27 18:25:46

标签: vue.js vuejs2 vue-component

我(仍在学习Vue,并且正在尝试使用带有产品选择器(Select2组件),一些税收计算(方法)和一些输入格式设置规则(Inputmask)的Vue2创建动态表。

一切正常,只有混合的子组件和指令无法按预期工作。

我正在使用Webpack,因此所有组件/指令都已导入。这是条目JS:

import DecimalMask from './directives/inputmask/decimal-mask';
new Vue({
el: '#vue-app',
components: {
    ....
    'select2-ajax': Select2Ajax,
    'select2-simple': Select2Simple,
    'dynamic-table': DynamicTable,
},
directives: {
    'price-mask': PriceMask,
    'decimal-mask': DecimalMask,
    'date-mask': DateMask,
    ....
}
});

这里有DynamicTables组件。

export default {
    props: {
        tableRows: {
            type: Array,
            default: function(){ return [{}] }
        }
    },
    data: function() {
        return {
            rows: this.tableRows
        }
    },
    computed: {
        total: function () {
            var t = 0;
            $.each(this.rows, function (i, e) {
                t += (e.price * e.qty);
            });
            return t;
        }
    },
    methods: {
        addRow: function () {
            try {
                this.rows.push({});
            } catch (e) {
                console.log(e);
            }
        },
        removeRow: function (index) {
            if(this.rows.length > 1)
                this.rows.splice(index, 1);
        }
    }
};

这是内联模板部分

...
<tr v-for="(row, index) in rows">
    <td>
        <select2-ajax
                inline-template
                v-model="row.product_id"
                ajax-source="{{ AURL::to('common/product-suggestion') }}">
            <select name="product[]" class="form-control">
            </select>
        </select2-ajax>
    </td>
    <td>
        <input v-decimal-mask class="form-control" name="qty[]" v-model="row.qty" number/>
    </td>
    <td>
        <input v-decimal-mask.price class="form-control text-right" name="price[]" v-model="row.price" number data-type="currency"/>
    </td>
    <td>
        <input v-decimal-mask.price class="form-control text-right" name="total[]" :value="row.qty * row.price" number readonly />
    </td>
    <td>
        <button type="button" class="btn btn-danger col-md-12" @click="removeRow(index)"><i class="fa fa-times"></i></button>
    </td>
</tr>
...

当前,我在DynamicTables组件中遇到以下错误:

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

  • 无法解析指令:十进制掩码

组件和指令在其他地方都可以完美地工作(在其他组件中不能混合使用),但是根据我的逻辑,它们应该在它们存在/存在于同一Vue实例中时起作用。谢谢!

1 个答案:

答案 0 :(得分:1)

您应该在全球范围内注册它们,以便在您的应用程序中随处使用它们:

import DecimalMask from './directives/inputmask/decimal-mask';
Vue.directive('decimal-mask',DecimalMask);
....
import customComponent from './Components/customComponent.vue'
Vue.component('custom-component',customComponent);
...
new Vue({...})
相关问题