【发布时间】:2021-04-21 13:20:49
【问题描述】:
我在 vuejs 中有一个模态,我通过传递 isActive 属性来打开/关闭,我想在用户单击后退按钮时关闭模态。
当模式打开时,我将哈希 #modal 添加到 url,当模式关闭时,我删除哈希。
我试图在用户单击后退按钮时关闭模式,但目前它没有,它只是删除了哈希。
如果我单击 close() 方法,模式将关闭,因此看起来 eventListener() 无法正常工作。
当用户点击后退按钮时如何关闭模态框?
modal.js
import { mapActions, mapGetters } from 'vuex';
export default {
props: {
isActive: {
type: Boolean,
default: false,
},
},
computed: {
hashChange() {
console.log(this.isActive)
if(this.isActive) {
window.location.assign('#modal')
} else {
history.replaceState({}, document.title, window.location.href.split('#')[0]);
}
},
},
methods: {
close() {
this.$emit('close');
},
},
mounted() {
window.addEventListener('hashchange', this.close());
},
destroyed() {
window.addEventListener('hashchange', this.close());
},
};
【问题讨论】:
标签: javascript vue.js