Skip to content

Template Refs

为了在Vue中访问模板的元素或视图,我们有template refs可用。

在NativeScript-Vue中,它与Vue完全相同,但有一个变化需要考虑,要访问原生视图,我们必须访问.nativeView响应式引用。例如:

vue
<script lang="ts" setup>
import { ref, onMounted } from "nativescript-vue"
import { type Label } from "@nativescript/core"

const el = ref();

onMounted(() => {
  const labelView = el.value.nativeView as Label;
  labelView.text= "Change Text";
})
</script>

<template>
  <Label ref="el" text="Template Refs" />
</template>