我的情况是这样的
我有两个组件,父组件和子组件
我的父组件是这样的:
<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>
如果模式显示,我选择大陆、国家和城市。然后我关闭模式
之后我再次显示模态。然后我先选择国家和城市,数据仍然存在
我想重置数据。因此,如果我再次打开模式,在选择大陆之前,国家和城市数据不会显示
I try :
Object.assign(this.$data, this.$options.data())
和这个 :
Object.assign(this.$data,this.$options.data.call(this))
还有这个:
this.$forceUpdate()
当模态隐藏时
但是,它不起作用
看来必须更新数据了computed:{...}
。但我仍然很困惑要这样做
我怎么解决这个问题?