开发了一个公共的Dropdown组件,要求全屏只有一个下拉激活(打开状态),如果有别的在打开则把它关闭。
export default {
data() {
return {
[this.name + '_isShow']: false,
}
},
computed: {
isShow: {
get() {
return this[this.name + '_isShow']
},
set(v) {
this[this.name + '_isShow'] = v
},
},
},
methods: {
toggleShow() {
this[this.name + '_isShow'] = !this[this.name + '_isShow']
},
closeShow() {
this.isShow = false
},
closeOtherDropdown() {
this.$bus.emit('closeOtherDropdown', this.name)
this.$bus.emit('closeMenuModal')
},
},
mounted() {
document.addEventListener('click', e => {
this.isShow && (this.isShow = false)
})
this.$bus.on('closeOtherDropdown', name => {
if (name !== this.name) {
this.isShow && (this.isShow = false)
}
})
},
beforeDestroy() {
this.$bus.$off('closeOtherDropdown')
this.$bus.$off('closeMenuModal')
},
}