首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >vee-validate客户规则的错误

vee-validate客户规则的错误
EN

Stack Overflow用户
提问于 2020-02-23 13:57:58
回答 1查看 1.9K关注 0票数 1

在@vue/cli 4.1.1应用程序中,我使用了v-money和vee-validate,我发现所需的规则对v-money不起作用,因为它的值始终为“0”。因此,我在这里编写了自定义验证http://vee-validate.logaretm.com/v2/guide/custom-rules.html#using-the-custom-rule

插入此检查[在测试页中,我在控制台中收到警告:

代码语言:javascript
复制
 WARNING  Compiled with 2 warnings                                                                                                                                                                           7:45:56 AM

 warning  in ./src/views/Test.vue?vue&type=script&lang=js&

"export 'Validator' was not found in 'vee-validate'

 warning  in ./src/views/Test.vue?vue&type=script&lang=js&

"export 'Validator' was not found in 'vee-validate'


  App running at:
  - Local:   http://localhost:8080/ 
  - Network: unavailable

在浏览器中,我看到了错误:

代码语言:javascript
复制
vue-router.esm.js?8c4f:2113 TypeError: Cannot read property 'extend' of undefined
    at eval (Test.vue?f246:87)
    at Module../node_modules/cache-loader/dist/cjs.js?!./node_modules/babel-loader/lib/index.js!./node_modules/cache-loader/dist/cjs.js?!./node_modules/vue-loader/lib/index.js?!./src/views/Test.vue?vue&type=script&lang=js& (4.js:11)
    at __webpack_require__ (app.js:790)
    at fn (app.js:151)

我的测试组件:

代码语言:javascript
复制
<template>
    <div class="frontend_item_container">
        <ValidationObserver
                ref="pageObserverForm"
                v-slot="{handleSubmit}"
        >
            <b-form @submit.prevent="handleSubmit(onSubmit)">

                <b-card-header>
                    <h3 class="row_content_left_aligned p-2" v-show="is_page_loaded">
                        <i :class="'info_link '+getHeaderIcon('page')"></i>{{ getHeaderTitle }}
                    </h3>
                    <div v-show="!is_page_loaded">
                        <h3>
                            <b-spinner variant="success" label="Page loading"></b-spinner>&nbsp;Page loading...
                        </h3>
                    </div>

                </b-card-header>

                <b-card-body v-show="is_page_loaded">

                    <b-row class="editor_row">
                        <b-col md="4">
                            <label for="editable_ad_price" class="pt-2 ">
                                Price<span class="required"> * </span>:
                            </label>
                        </b-col>
                        <b-col md="8">
                            price::{{ price}}
                            <ValidationProvider
                                    name="editable_ad_price"
                                    rules="required|truthy"
                                    v-slot="{ errors }"
                            >
                                <money
                                        v-model="price"
                                        v-bind="moneyConfig"
                                        name="editable_ad_price"
                                        id="editable_ad_price"
                                        class="form-control text-right"
                                        placeholder="Enter price"
                                >
                                </money>
                                <p class="validation_error">{{ clearErrorMessage(errors[0]) }}</p>
                            </ValidationProvider>
                        </b-col>
                    </b-row>

                </b-card-body>

                <b-card-footer class="buttons_container" v-show="is_page_loaded">
                    <b-button size="md" @click.prevent="$router.push('/admin/pages')" class="m-3">
                        <i :class="'a_link '+getHeaderIcon('cancel')"></i>Cancel
                    </b-button>
                    <b-button type="submit" size="md" variant="success" class="m-3">
                        <i :class="'action_link '+getHeaderIcon('save')"></i>{{ submit_label }}
                    </b-button>
                </b-card-footer>

            </b-form>
        </ValidationObserver>
    </div>
</template>

<script>
    import appMixin from '@/appMixin';
    import Vue from 'vue'

    import money from 'v-money'
    Vue.use(money, {precision: 4})

    import {settingsLocalizeMessages} from '@/app.settings.js'

    import {ValidationObserver, ValidationProvider, extend} from 'vee-validate'

    import * as rules from 'vee-validate/dist/rules';

    Object.keys(rules).forEach(rule => {
        extend(rule, rules[rule]);
    });


    import { Validator } from 'vee-validate';

    Validator.extend('truthy', {
        getMessage: field => 'The ' + field + ' value is not truthy.',
        validate: value => !! value
    });

    let instance = new Validator({ trueField: 'truthy' });

    instance.extend('falsy', (value) => ! value);

    instance.attach({
        name: 'falseField',
        rules: 'falsy'
    });


    import {localize} from 'vee-validate';
    localize({
        en: settingsLocalizeMessages['en']
    });

    export default {
        data() {

            return {
                apiUrl: process.env.VUE_APP_API_URL,
                price: 12,
                moneyConfig: {
                    decimal: ',',
                    thousands: '.',
                    prefix: '$',
                    suffix: '',
                    precision: 2,
                    masked: false
                },
                is_page_loaded: false,

            }
        }, // data() {

        name: 'testPage',
        mixins: [appMixin],

        components: {
            ValidationObserver, ValidationProvider
        },

        mounted() {
        }, //  mounted() {

        created() {
        }, //  created() {

        beforeDestroy() {
        },

        methods: {
        },  // methods: {

        computed: {
            getHeaderTitle: function () {
                return 'Test'
            },
        }, //computed: {


    }
</script>

为什么会出现错误以及如何修复?

代码语言:javascript
复制
"bootstrap-vue": "^2.3.0",
"v-money": "^0.8.1",
"vee-validate": "^3.2.1",
"vue": "^2.6.11",

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-25 01:03:35

您使用的是vee-validate的旧文档。在版本3中,您必须执行以下操作:

代码语言:javascript
复制
import { extend } from 'vee-validate';

也可以在这里查看3.x文档:https://logaretm.github.io/vee-validate/guide/basics.html#adding-rules

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60359606

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档