如果模态在VUE组件上关闭,我该如何重置下拉数据



我的案例

我有两个组成部分,父母和子组件

我的父组件:

<template>
    ...
    <div class="row">
        ...
            <location-select level="continentList" type="1"/>
        ...
            <location-select level="countryList" type="2"/>
        ...
            <location-select level="cityList" type="3"/>
        ...
    </div>
</template>
<script>
    ...
    export default{
        ...
    }
</script>

父组件是模态bootstrap

我的孩子组成部分:

<template>
    <select class="form-control" v-model="selected" @change="changeLocation">
        <option value="0" disabled>Select</option>
        <template v-for="option in options">
            <option v-bind:value="option.id" >{{ option.name }}</option>
        </template>
    </select>
</template>
<script>
    ...
    export default{
        props: ['level','type'],
        data() { return { selected: '' };},
        computed:{
            ...mapGetters([
                'getContinentList', 'getCountryList','getCityList'
            ]),
            options(){
                const n = ['getContinentList', 'getCountryList','getCityList']
                return this[n[this.type-1]]
            }
        },
        methods:{
            ...mapActions([
                'getLocationList'
            ]),
            changeLocation(event){
                if(this.type==1){
                    this.getLocationList([{type:2},{level:'countryList'}])
                    this.getLocationList([{type:3},{level:'cityList'}])
                }else if(this.type==2){
                    this.getLocationList([{type:3},{level:'cityList'}])
                }
            }
        },
        created() {
            if(this.type==1)
                this.getLocationList([{type:1},{level:'continentList'}])
            if(this.type==2 && this.selected!='')
                this.getLocationList([{type:2},{level:'countryList'}])
            if(this.type==3 && this.selected!='')
                this.getLocationList([{type:3},{level:'cityList'}])
        },
        mounted() {
            $(this.$parent.$refs.modal).on('hidden.bs.modal', (e) => {
                Object.assign(this.$data, this.$options.data())
            })
        },
    };
</script>

如果模态秀,我选择了大陆,乡村和城市。然后我关闭模态

之后,我再次显示模式。然后我首先选择国家和城市,数据仍然存在

我想重置数据。因此,如果我再次打开模式,在我选择大陆之前,国家和城市数据未显示

我尝试:

Object.assign(this.$data, this.$options.data())

和此:

Object.assign(this.$data,this.$options.data.call(this))

也是如此:

this.$forceUpdate()

模态隐藏

但是,它不起作用

似乎必须更新数据computed:{...}。但是我仍然为此感到困惑

如何解决此问题?

您是否尝试过重置mounted LifeCycle Hook中的selected数据属性?

...    
mounted() {
    let $vm = this;
    $(this.$parent.$refs.modal).on('hidden.bs.modal', (e) => {
         $vm.selected = '';
    });
},
...

只是v-if f-if模态组件,切换时它将恢复/重新计算所有内容。

父母的父母:

    <template>
        <div class="parent-of-parent">
            <your-modal-component v-if="isModalOpened" @close="closeModal"></your-modal-component>
        </div>
    </template>

<script>
   export default{
        data() { 
            return { isModalOpened: false };
        },
        methods: {
           openModal() {
               this.isModalOpened = true;
           },  // method to open it
           closeModal() {
              this.isModalOpened = false;
           },   // method to attach to emit event from modal on close

        },
    };
</script>

请确保将单击事件连接到您单击的图标上,以获取关闭或其他内容。并将一种方法附加到这样的事件中。

this.$emit('closeModal');

如果您在动画模态有问题的情况下使用<transition>

<transition name="fadeIn">
     <your-modal-component v-if="isModalOpened" @close="closeModal"></your-modal-component>
</transition>

最新更新