Javascript LitElement如何计算元素的宽度

Javascript LitElement如何计算元素的宽度,javascript,shadow-dom,lit-element,Javascript,Shadow Dom,Lit Element,如何在Lit元素中找到HTML元素的宽度知道吗,我找不到如何使用@query import { LitElement, html, query } from 'lit-element'; class MyElement extends LitElement { @query('#first') first; render() { return html` <div id="first">I want to know the si

如何在Lit元素中找到HTML元素的宽度知道吗,我找不到如何使用@query

import { LitElement, html, query } from 'lit-element';

class MyElement extends LitElement {
  @query('#first')
  first;

  render() {
    return html`
      <div id="first">I want to know the size of this div</div>
      <div id="second">and set size to this div</div>
    `;
  }
}
从'lit element'导入{LitElement,html,query};
类MyElement扩展了LitElement{
@查询(“#第一个”)
第一;
render(){
返回html`
我想知道这个房间的大小
并将大小设置为该div
`;
}
}
您可以使用函数获取任何元素的宽度和任何其他CSS属性

import { LitElement, html, query } from 'lit-element';

class MyElement extends LitElement {    
  render() {
    return html`
      <div id="first">I want to know the size of this div</div>
      <div id="second">and set size to this div</div>
    `;
  }

  updated() {
     const elem = this.shadowRoot.querySelector('#first');
     this._width = getComputedStyle(elem).getPropertyValue('width');
     console.log(this._width);
  }
}
从'lit element'导入{LitElement,html,query};
MyElement类扩展了LitElement{
render(){
返回html`
我想知道这个房间的大小
并将大小设置为该div
`;
}
更新的(){
const elem=this.shadowRoot.querySelector(“#first”);
这个._width=getComputedStyle(elem).getPropertyValue('width');
console.log(此宽度);
}
}

@query decorator仅在typescript中工作,是否使用typescript?
const width=this.shadowRoot.querySelector(“#first”).clientWidth