jQuery方法上的对象可能未定义

时间:2019-06-20 14:35:19

标签: jquery typescript vue.js

我正在尝试为用户到达窗口底部添加一种方法,但是打字稿阻止了我,并说该对象可能在window.scrollTop(),height()和document.height( )。

我已经尝试过事先放置一个if语句,并且仅在定义了代码的情况下才运行它,但是我一直收到错误消息。

mounted() {
    // Does not work because object might be undefined
   $(window).scroll(() => {
     if (
       $(window).scrollTop() + $(window).height() >
       $(document).height() - 100
     ) {
       console.log(this.lastCursor);
     }
   });
 },

1 个答案:

答案 0 :(得分:0)

尝试一下:

mounted() {
   this.lastCursor = 0;
   if ($(window) && )
    // Does not work because object might be undefined
   $(window).scroll(() => {
     if (
       $(window).scrollTop() + $(window).height() >
       $(document).height() - 100
     ) {
       console.log(this.lastCursor);
     }
   });
 },

我试图消除所有可能不确定的情况。如果可行,请尝试确定未定义的变量。