小编zur*_*mai的帖子

Vue 3模板引用动态名称

我注意到,要在 Vue 3 组合 api 中创建模板引用<script setup>,我应该创建一个与引用值完全相同的变量名称。

例如,在Vue 3 文档中:

<template>
  <div ref="root">This is a root element</div>   <--- notice the ref name "root"
</template>

<script>
  import { ref, onMounted } from 'vue'

  export default {
    setup() {
      const root = ref(null)   <---- and the variable name should be "root" too

      onMounted(() => {
        console.log(root.value) 
      })

      return {
        root
      }
    }
  }
</script>
Run Code Online (Sandbox Code Playgroud)

我想知道是否可以像 Vue 2 那样使用 ref ,因为我有一些元素,其 ref 是随机字符串,并且它只需在$refs对象上传递名称即可在 Vue 2 上工作。

<div …
Run Code Online (Sandbox Code Playgroud)

vue.js vuejs3 vue-composition-api vue-script-setup

4
推荐指数
1
解决办法
1万
查看次数