ERROR TypeError: Cannot read property 'call' of undefined

ERROR TypeError: Cannot read property 'call' of undefined

我终于开始在我的 angular 9 项目中制作 nouislider 了。我现在的问题是我想在滑块值更改时更新一个字段(因此当用户移动滑块时,我想在屏幕上显示新值)。活动有问题,我不确定是什么问题。

noUiSlider.create(this.theHtmlElement, {
  start: [3000],
  step: 1,
  connect: [true, false],
  range: {
      'min': [0],
      'max': [10000]
  }
});

this.theHtmlElement.noUiSlider.on('update', this.valueChanged());


...
valueChanged() {
console.log(this.theHtmlElement.noUiSlider.get());

}

我得到的错误是:

ERROR TypeError: Cannot read property 'call' of undefined
at nouislider.js:2082
at Array.forEach (<anonymous>)
at nouislider.js:2081
at Array.forEach (<anonymous>)
at fireEvent (nouislider.js:2077)
at nouislider.js:2055
at Array.forEach (<anonymous>)
at Object.bindEvent [as on] (nouislider.js:2054)
at AddCreditPageComponent.ngAfterViewInit (add-credit-page.component.ts:65)
at callProviderLifecycles (core.js:33986)

该事件似乎第一次成功,我在控制台中看到了初始值,但随后出现了错误!然后没有别的工作。我走了一圈,似乎有第二个范围事件未定义或类似的东西!不确定它是什么以及它来自哪里,可能是导致问题的原因。我不确定如何修复它。

要绑定一个事件,您必须传入一个匿名函数(还有其他可能性);

this.theHtmlElement.noUiSlider.on('update', () => this.valueChanged());

this.theHtmlElement.noUiSlider.on('update', this.valueChanged.bind(this));

this.theHtmlElement.noUiSlider.on('update', this.valueChanged);

readonly valueChanged = () => {
  console.log(this.theHtmlElement.noUiSlider.get());
};

您是如何做到的,它会立即执行 this.valueChanged() 方法,即 returns undefined。然后它尝试将此 undefined 绑定到 update 事件处理程序,这将导致您看到的错误