【问题标题】:Nested this in Stencil.js components将此嵌套在 Stencil.js 组件中
【发布时间】:2020-08-09 06:49:15
【问题描述】:

我想知道是否有更简单的方法可以在 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>;
  }

但我总是重复自己写一个嵌套的这个变量,如上所示。

有没有更优雅的方式来做我需要的事情?

【问题讨论】:

    标签: javascript typescript jsx stenciljs tsx


    【解决方案1】:

    使用箭头功能:

    render() {
        return <Host>
            {this.images ?
    
              this.imagesArray.map((el) => (<img
                  // @ts-ignore
                  src={this.imageSize || el.url}/>
              )) : <slot/>}
          </div>
        </Host>;
      }

    【讨论】:

      猜你喜欢
      • 2017-12-31
      • 1970-01-01
      • 1970-01-01
      • 2017-02-12
      • 2022-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多