自定义元素/Web 组件方法作为箭头函数[重复]

问题描述 投票:0回答:0

我正在学习自定义元素/网络组件,我不确定为什么我的 connectedCallback() 和 attributeChangedCallback() 方法似乎从未被调用并且没有收到任何错误,但最终发现我将它们中的每一个都创建为箭头功能。将它们更改为常规功能让事情再次开始。

为什么 connectedCallback 和 attributeChangedCallback() 不能用作箭头函数?作为箭头函数是否会以无法在自定义元素中使用的方式改变范围?这与 Web 组件中“this”的使用有什么关系,如果 Web 组件中的other 方法 箭头函数,这会是一个问题吗? (< this seems to work fine, but will my this from the arrow functions be somehow a higher level than the this from the non-arrow connectedCallback etc?)

javascript this web-component arrow-functions custom-element
© www.soinside.com 2019 - 2024. All rights reserved.