2017-09-14 80 views
2

我的情況是這樣如何在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> 

父組件是一個模式引導

我的子組件像這樣:

<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:{...}。但我仍然很困惑

我該如何解決這個問題?

回答

0

您是否嘗試過重置mounted生命週期掛鉤中的selected數據屬性?

...  
mounted() { 
    let $vm = this; 

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

這是相同的。它不起作用 –

+0

是否觸發了「hidden.bs.modal」事件? –

+0

是的。如果我添加'console.log('test')',它會出現如果模態關閉 –

0

只是v - 如果模態組件,它會重新切換/重新計算切換時的所有內容。

父父組件的:

<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> 
相關問題