【问题标题】:Update initial value. Vee validate does not detect changes?更新初始值。 Vee validate 未检测到更改?
【发布时间】:2020-12-13 06:00:52
【问题描述】:

我有两个表单域:国家和地区。首先,你必须选择一个国家而不是一个地区。如果选择了一个国家,我会将地区(选项)添加到下一个组件:Region

我用 axios 为区域添加选项。

我遇到问题的场景:当用户选择一个国家/地区时,一个地区然后返回更改国家/地区。 该区域需要重置。我尝试使用生命周期挂钩 mountedcreated 来做到这一点,但 VeeValidate 没有检测到任何变化。因此该值已正确更改,但VeeValidate 未检测到它。我通过在mounted 中添加watch(和nextTick)解决了这个问题,但我怀疑这是一个很好的解决方案。有没有人有其他想法?

我对 Vue.js 的经验很少。任何帮助表示赞赏。

这是我完整的SelectBox.vue

<template>
    <div>
        <v-select
            @input="setSelected"
            :options="choices"
            :filterable="filterable"
            :value="value"
            :placeholder="placeholder"
        ></v-select>
    </div>
</template>

<script>
import vSelect from "vue-select";
import debounce from 'lodash/debounce';
import axios from 'axios';

//axios.defaults.headers.get["Vuejs"] = 1;
export default {
    props: [
        "options", "filterable",
        "value", "url", 
        "name", "placeholder"
    ],
    data: function () {
        var choices = this.options;
        if (this.name == "region") {
            choices = this.$store.state["regionChoices"] || [];
            if (this.value && !choices.includes(this.value)) {
                this.toReset = true;
            }
            if ( Array.isArray(choices) && choices.length < 1) { 
                this.addError("region", "Country is not selected or there are not available options for your selected country.");
            }
        }
        return {
            choices: choices
            // refPrefix: this.name + "Ref"
        }
    },
    components:{
        vSelect
    },
    inject: ["addError"],
    methods: {
        searchRegions: debounce((val, vm) => {
            axios.get(vm.url + val)
            .then(res => {
                vm.$store.state["regionChoices"] = res.data.results;
            });
        }, 250),
        setSelected(val) {
            this.$emit("input", val);
            if (this.name == "country") {
                const countryId = val ? val.value : "0";
                this.searchRegions(countryId, this);
            }
        },
    },
    mounted() {
        if (this.name == "region") {
            this.$watch('value', function(value) {
                if (this.toReset) {
                    this.$nextTick(function () {
                        this.setSelected("");
                        this.toReset = value ? true : false;
                    });
                }
            }, { immediate: true });
        }
    },
}
</script>

【问题讨论】:

    标签: vue.js vue-cli vee-validate vue-select


    【解决方案1】:

    我的 Vue 模型是在父组件中定义的。所以我的模型属性也必须从我的组件的父级更新。我将方法resetRegion 添加到我的父组件中。所以我可以在我的子组件中使用它,provideinject

    我的父组件的代码sn-p:

    export default {
        name: "form-template",
        components: {
            FieldGroup,
        },
        provide() {
            return {
                addError: this.addError,
                resetRegion: this.resetRegion
            }
        },
        methods: {
            ...mapMutations({
                updateField: "applicant/updateField"
            }),
            addError(field, msg) {
                this.errors.add({field: field, msg: msg});
            },
            resetRegion() {
                this.formData.region = "";
            }
        }
    }
    

    我的子组件的代码sn-p:

    export default {
        props: [
            "options", "value", "multiple",
            "name", "placeholder", "url"
        ],
        components:{
            vSelect
        },
        inject: ["addError", "resetRegion"],
        computed: {
            choices: function () {
                let choices = this.options;
                if (this.name == "region") {
                    choices = this.$store.state["regionChoices"];
                }
                return choices || []
            }
        },
        methods: {
            searchRegions(val, vm) {
                vm.$store.state["regionChoices"] = [];
                axios.get(vm.url + "?pk=" + val)
                .then(res => {
                    const choices = res.data.results || [];
                    vm.$store.state["regionChoices"] = choices;
                    if (Array.isArray(choices)) {
                        const curValue = vm.$store.state.lead.formData.region;
                        if (choices.length < 1 || (curValue && !choices.some(e => e.id === curValue))) {
                            this.resetRegion();
                        }
                    } else {
                        this.resetRegion();
                    }
                });
            },
            setSelected(val) {
                this.$emit("input", val);
                if (this.name == "country") {
                    this.searchRegions(val, this);
                }
            }
        },
        mounted() {
            if (this.name == "region") {
                if(!Array.isArray(this.choices) || this.choices.length < 1) {
                    this.addError("region", window.HNJLib.localeTrans.regionErr);
                }
            }
        },
        created() {
            if (this.name == "country" && this.value && !this.$store.state.hasOwnProperty("regionChoices")) {
                this.searchRegions(this.value, this);
            }
        }
    }
    

    这样我的问题就解决了。

    【讨论】:

      猜你喜欢
      • 2019-01-15
      • 2019-08-14
      • 2022-10-05
      • 1970-01-01
      • 2018-12-01
      • 1970-01-01
      • 2015-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多