【问题标题】:Object is possibly undefined on jquery methods对象可能在 jquery 方法上未定义
【发布时间】:2019-11-03 09:55:01
【问题描述】:

我正在尝试添加一种方法,用于当用户到达窗口底部时,但打字稿阻止了我并说该对象可能在 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);
     }
   });
 },

【问题讨论】:

  • 所以未定义在窗口上,而不是this.lastCursor?

标签: jquery typescript vue.js


【解决方案1】:

试试这个:

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);
     }
   });
 },

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-02
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2020-08-11
    • 1970-01-01
    相关资源
    最近更新 更多