From bd0584f319b54f2389ae26ca0b94a428e0235dfd Mon Sep 17 00:00:00 2001 From: 07akioni <07akioni2@gmail.com> Date: Tue, 19 May 2020 00:10:51 +0800 Subject: [PATCH] perf(mixin/placeable): register listeners on demand --- src/_mixins/placeable.js | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/_mixins/placeable.js b/src/_mixins/placeable.js index d130e331b..b5d6839d3 100644 --- a/src/_mixins/placeable.js +++ b/src/_mixins/placeable.js @@ -1,6 +1,5 @@ import scrollDelegate from '../_utils/delegate/scrollDelegate' import resizeDelegate from '../_utils/delegate/resizeDelegate' -import getParentNode from '../_utils/dom/getParentNode' import getScrollParent from '../_utils/dom/getScrollParent' import { getAdjustedPlacementOfTrackingElement, @@ -169,6 +168,11 @@ export default { watch: { active (value) { if (value) { + if (this.listenersRegistered) { + this.registerScrollListeners() + this.registerResizeListener() + this.listenersRegistered = true + } this.$nextTick().then(this.updatePosition) } }, @@ -184,17 +188,23 @@ export default { trackingElement: null, trackedElement: null, scrollListeners: [], - adjustedPlacement: this.placement + adjustedPlacement: this.placement, + listenersRegistered: false } }, mounted () { - this.registerScrollListeners() - this.registerResizeListener() + if (this.active) { + this.registerScrollListeners() + this.registerResizeListener() + this.listenersRegistered = true + } this.updatePosition() }, beforeDestroy () { - this.unregisterScrollListeners() - this.unregisterResizeListener() + if (this.listenersRegistered) { + this.unregisterScrollListeners() + this.unregisterResizeListener() + } }, methods: { _getTrackingElement () {