【发布时间】:2020-05-03 16:47:55
【问题描述】:
这是我第一次使用 IntersectionObserver,我关注了这个文档https://www.netguru.com/codestories/infinite-scroll-with-vue.js-and-intersection-observer .但是因为这个错误我被阻止了
[Vue warn]: Error in mounted hook: "TypeError: Failed to construct 'IntersectionObserver': The provided value is not of type '(Element or Document)'"
这是我的触发器组件
<template>
<span ref='trigger'></span>
</template>
<script>
export default {
props:{
options:{
type: Object,
default: () => ({
root: 0,
threshold: "0",
})
}
},
data(){
return{
observer : null
}
},
mounted(){
this.observer = new IntersectionObserver( entries => {
this.handleIntersect(entries[0]);
}, this.options);
this.observer.observe(this.$refs.trigger);
},
destroyed(){
this.observer.disconnect();
},
methods:{
handleIntersect(entry){
if (entry.isIntersecting) this.$emit("triggerIntersected");
}
}
}
</script>
我该如何解决这个问题?(谢谢)
【问题讨论】:
-
您使用的是哪个浏览器和版本?
-
谷歌浏览器 81.0.4044.129
-
您是否在
created生命周期方法中尝试过此操作,而不是mounted? -
是的,同样的错误
-
您的 IntersectionObserver 似乎未定义或未正确导入。
标签: javascript vue.js vue-component intersection-observer