如何在TypeScript代码里获得HTML元素的css属性

99 阅读1分钟

首先从angular/core里导入ElementRef:

然后注入到构造函数中:

最后通过如下代码访问名为–cx-max-views的CSS属性:

/**
   * Returns the maximum number of views per split-view. The number is based on the
   * CSS custom property `--cx-max-views`.
   */
  protected get splitViewCount(): number {

    console.log('Jerry cod19: ');
    return Number(
      getComputedStyle(this.elementRef.nativeElement).getPropertyValue(
        '--cx-max-views'
      )
    );
  }


更多Jerry的原创文章,尽在:“汪子熙”: