From 20bcbf18cb59bcf603b762240ad67607fbdc73a5 Mon Sep 17 00:00:00 2001 From: "Victor.Sergeyev" Date: Fri, 26 Aug 2022 19:45:38 +0400 Subject: [PATCH] fixed: handle dynamically loaded components --- src/index.js | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/index.js b/src/index.js index aa8bfb4..e9e1821 100644 --- a/src/index.js +++ b/src/index.js @@ -41,16 +41,7 @@ export default { rootMargin: this.rootMargin }) - this.$nextTick(() => { - if (this.$slots.default && this.$slots.default.length > 1) { - warn('[VueIntersect] You may only wrap one element in a component.') - } else if (!this.$slots.default || this.$slots.default.length < 1) { - warn('[VueIntersect] You must have one child inside a component.') - return - } - - this.observer.observe(this.$slots.default[0].elm) - }) + this.observe() }, destroyed () { this.$emit('destroyed') @@ -58,5 +49,30 @@ export default { }, render () { return this.$slots.default ? this.$slots.default[0] : null + }, + methods: { + observe() { + this.$nextTick(() => { + const slot = this.$slots.default + + if (slot && slot.length > 1) { + warn('[VueIntersect] You may only wrap one element in a component.') + } else if (!slot || slot.length < 1) { + warn('[VueIntersect] You must have one child inside a component.') + return + } + + const vNode = slot[0] + + if (vNode.asyncFactory && !vNode.asyncFactory.resolved) { + vNode.asyncFactory().then(() => { + this.observe() + }) + } else { + const { elm } = vNode + this.observer.observe(elm) + } + }) + } } }