Import custom MutationObserver; remove mutation observer before component get destroyed

This commit is contained in:
Kang Cheng 2017-10-30 00:33:31 -07:00
parent 79885751e2
commit 3be0aa121c

View file

@ -26,7 +26,7 @@
<script> <script>
import Icon from '../icon/icon.vue'; import Icon from '../icon/icon.vue';
import Render from '../base/render'; import Render from '../base/render';
import { oneOf } from '../../utils/assist'; import { oneOf, MutationObserver } from '../../utils/assist';
import Emitter from '../../mixins/emitter'; import Emitter from '../../mixins/emitter';
import elementResizeDetectorMaker from 'element-resize-detector'; import elementResizeDetectorMaker from 'element-resize-detector';
@ -342,18 +342,19 @@
const hiddenParentNode = this.isInsideHiddenElement(); const hiddenParentNode = this.isInsideHiddenElement();
if (hiddenParentNode) { if (hiddenParentNode) {
const mutationObserver = new MutationObserver(() => { this.mutationObserver = new MutationObserver(() => {
if (hiddenParentNode.style.display !== 'none') { if (hiddenParentNode.style.display !== 'none') {
this.updateBar(); this.updateBar();
mutationObserver.disconnect(); this.mutationObserver.disconnect();
} }
}); });
mutationObserver.observe(hiddenParentNode, { attributes: true, childList: true, characterData: true, attributeFilter: ['style'] }); this.mutationObserver.observe(hiddenParentNode, { attributes: true, childList: true, characterData: true, attributeFilter: ['style'] });
} }
}, },
beforeDestroy() { beforeDestroy() {
this.observer.removeListener(this.$refs.navWrap, this.handleResize); this.observer.removeListener(this.$refs.navWrap, this.handleResize);
this.mutationObserver.disconnect();
} }
}; };
</script> </script>