我想知道是否有更简单的方法在Stencil.js组件中使用嵌套的“ this”。
此刻我正在这样做:
render() {
let thisNested = this;
return <Host>
{this.images ?
this.imagesArray.map(function (el) {
return <img
// @ts-ignore
src={thisNested.imageSize ? thisNested.imageSize : el.url}/>
}) : <slot/>}
</div>
</Host>;
}
但是我总是重复自己写一个嵌套的变量,如上所示。
有没有更优雅的方式来做我需要的事情?
答案 0 :(得分:1)
使用箭头功能:
render() {
return <Host>
{this.images ?
this.imagesArray.map((el) => (<img
// @ts-ignore
src={this.imageSize || el.url}/>
)) : <slot/>}
</div>
</Host>;
}