如何在 Nativescript 中获取 elementById?

sta*_*rsp 4 javascript vue.js nativescript nativescript-vue

我正在将 nativescript 与 vue.js 一起使用,并且正在尝试执行诸如 DOM 操作之类的操作。这是我的模板中的示例代码:

<Label textWrap="true">
    <FormattedString id="formString"
      backgroundColor="yellow"
      effectiveHeight="100"
      effectiveWidth="100%">
      <Span text="This text has a " /> 
    </FormattedString>
 </Label>
Run Code Online (Sandbox Code Playgroud)

我想通过他的 id获取标签元素FormattedString - formString 在 javascript 中是这样的:

let fs = doument.getElementById('formString');
我怎样才能在 Nativescript-Vue 中做到这一点?我知道有库nativescript-dom,但我不想将整个库用于简单的 getById。

Rac*_*len 5

使用Label.getViewById("formString")访问FormattedString的属性。

getViewById是与 javascript getElementById 最接近的等价物

希望这可以帮助

  • 这完全有效,但如果你在 Vue 上,那么使用 `ref` 而不是 `id` 可能是首选。 (2认同)