1

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

我使用 axios 为区域添加选项。

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

我对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>
4

1 回答 1

0

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

所以我的问题解决了。

于 2020-09-29T17:25:24.103 回答