react antd 组件中引入外部对象的方法,外部对象的方法如何互调

新手上路,请多包涵

1 组件中引入image.png saleOnLineUtils 对象
2 在saleOnLineUtils 对象中调用自己的方法,报错
image.png
image.png

阅读 2.2k
1 个回答

看你的代码 saleOnLineUtils 应该是一个class组件。
常见的 this 指向问题,在 onProductChange 方法中 this不是指向组件实例了。
解决方案如下:

  1. 实例方法全部使用箭头函数定义,如:
  const onSaleChange = ()=>{/** your code */}
  const onProductChange = ()=>{/** your code */}
  const clearProductContent = ()=>{/** your code */}
  1. 在constructor 中给实例方法绑定 this :
  constructor(props) {
    super(props)
    this.onProductChange = this.onProductChange.bind(this)
    /** 其他实例方法也是*/
  }

推荐用箭头函数

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题