当前位置:首页 > VUE

vue ref实现

2026-03-27 19:33:29VUE

Vue 中使用 ref 实现

在 Vue 中,ref 是一种用于获取 DOM 元素或组件实例引用的方法。它可以在模板中直接使用,也可以在组合式 API 中使用。

在模板中使用 ref

在模板中,可以通过 ref 属性标记一个 DOM 元素或子组件,随后在组件实例中通过 this.$refs 访问。

<template>
  <div>
    <input ref="inputRef" type="text" />
    <button @click="focusInput">Focus Input</button>
  </div>
</template>

<script>
export default {
  methods: {
    focusInput() {
      this.$refs.inputRef.focus();
    }
  }
};
</script>

在组合式 API 中使用 ref

在 Vue 3 的组合式 API 中,ref 不仅可以用于模板引用,还可以用于创建响应式数据。

vue ref实现

<template>
  <div>
    <input ref="inputRef" type="text" />
    <button @click="focusInput">Focus Input</button>
  </div>
</template>

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

export default {
  setup() {
    const inputRef = ref(null);

    onMounted(() => {
      console.log(inputRef.value); // 获取 DOM 元素
    });

    const focusInput = () => {
      inputRef.value.focus();
    };

    return {
      inputRef,
      focusInput
    };
  }
};
</script>

使用 ref 创建响应式数据

ref 还可以用于创建响应式的基本类型数据(如字符串、数字等)。

<template>
  <div>
    <p>{{ count }}</p>
    <button @click="increment">Increment</button>
  </div>
</template>

<script>
import { ref } from 'vue';

export default {
  setup() {
    const count = ref(0);

    const increment = () => {
      count.value++;
    };

    return {
      count,
      increment
    };
  }
};
</script>

ref 与 reactive 的区别

  • ref 用于包装基本类型数据(如数字、字符串),通过 .value 访问。
  • reactive 用于包装对象或数组,可以直接访问属性。
import { ref, reactive } from 'vue';

const count = ref(0); // 访问 count.value
const state = reactive({ count: 0 }); // 直接访问 state.count

在子组件中使用 ref

通过 ref 可以获取子组件的实例,调用其方法或访问其数据。

vue ref实现

<template>
  <ChildComponent ref="childRef" />
  <button @click="callChildMethod">Call Child Method</button>
</template>

<script>
import { ref } from 'vue';
import ChildComponent from './ChildComponent.vue';

export default {
  components: { ChildComponent },
  setup() {
    const childRef = ref(null);

    const callChildMethod = () => {
      childRef.value.someMethod();
    };

    return {
      childRef,
      callChildMethod
    };
  }
};
</script>

动态 ref

可以通过动态绑定 ref 来灵活地引用多个元素。

<template>
  <div v-for="item in items" :key="item.id" :ref="setItemRef">
    {{ item.text }}
  </div>
</template>

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

export default {
  setup() {
    const items = ref([{ id: 1, text: 'Item 1' }, { id: 2, text: 'Item 2' }]);
    const itemRefs = ref([]);

    const setItemRef = (el) => {
      if (el) {
        itemRefs.value.push(el);
      }
    };

    onMounted(() => {
      console.log(itemRefs.value); // 所有动态引用的 DOM 元素
    });

    return {
      items,
      setItemRef
    };
  }
};
</script>

总结

ref 在 Vue 中有多种用途:

  • 获取 DOM 元素或组件实例。
  • 创建响应式数据(基本类型)。
  • 动态绑定多个引用。

通过合理使用 ref,可以更灵活地操作 DOM 或组件实例。

标签: vueref
分享给朋友:

相关文章

vue 实现拖拽

vue 实现拖拽

Vue 实现拖拽功能 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性和相关事件实现拖拽功能。 <template> &l…

vue SSG实现

vue SSG实现

Vue SSG 实现方法 Vue 的静态站点生成(SSG)可以通过多种工具和框架实现,以下是几种常见的方法: 使用 VuePress VuePress 是一个基于 Vue 的静态站点生成器,适合文档…

vue怎么实现

vue怎么实现

Vue 实现方法 Vue 是一个流行的前端框架,用于构建用户界面。以下是几种常见的实现方法: 数据绑定 使用 v-model 指令实现双向数据绑定,适用于表单输入元素。 <input v-m…

vue实现签章

vue实现签章

Vue 实现签章功能 签章功能通常包括手写签名、电子印章等。以下是基于 Vue 的实现方法: 使用 canvas 实现手写签名 安装依赖(如需要): npm install signatu…

vue实现京东

vue实现京东

Vue 实现京东电商网站的关键步骤 项目搭建与基础配置 使用 Vue CLI 或 Vite 初始化项目,安装 Vue Router 管理路由,Vuex/Pinia 管理状态。配置基础 UI 框架如 E…

vue远离实现

vue远离实现

Vue 原理实现 Vue.js 的核心原理包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是其关键实现原理的详细解析。 响应式系统 Vue 的响应式系统基于 Object.definePro…