vue3 模板引用
基础
1 | <input ref="myInput" /> |
只能在组件挂载后访问模板引用,因为初次渲染时模板引用为 null
比如:
1 | watchEffect(() => { |
列表引用
1 | <script setup> |
函数模板引用
每次组件更新时会调用 handleRef:
1 | <input :ref="handleRef(el)" /> |
组件上的 ref
引用获得的值是组件实例:
1 | <script setup> |
父组件对子组件访问权
1. 选项式 API 或 不使用 <script setup>
父组件对子组件的所有属性和方法有完全的访问权(大多情况下应使用 props 和 emit)。
2. 使用 <script setup>
子组件默认私有,除非子组件在其中通过 defineExpose 宏显示暴露。
1 | <script setup> |
- Title: vue3 模板引用
- Author: Gabrielle
- Created at : 2023-12-08 18:22:28
- Updated at : 2025-05-20 10:47:28
- Link: https://zoella-w.github.io/2023/12/08/13-vue3模板引用/
- License: This work is licensed under CC BY-NC-SA 4.0.