更新初始值。 Vee 验证不检测更改?

Update initial value. Vee validate does not detect changes?

我有两个表单域:国家和地区。首先,你必须选择一个国家而不是一个地区。如果选择了一个国家/地区,我将区域(选项)添加到下一个组件: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 模型是在父组件中定义的。所以我的模型 属性 也必须从我的组件的父级更新。我将方法 resetRegion 添加到我的父组件中。所以我可以在我的子组件中使用它 provideinject.

我的父组件代码片段:

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 = "";
        }
    }
}

我的子组件代码片段:

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);
        }
    }
}

所以我的问题就解决了。