- <div ref=“Ref”></div>
- this.$refs.Ref
- <template>
- <div ref=“Ref”>获取单个DOM元素</div>
- </template>
- <script>
- import { ref, onMounted } from ‘vue’;
- export default {
- setup() {
- const Ref = ref(null);
- onMounted(() => {
- console.dir(Ref.value);
- });
- return {
- Ref
- };
- }
- };
- </script>
- <template>
- <div>获取多DOM元素</div>
- <ul>
- <li v-for=“(item, index) in arr” :key=“index” :ref=“setRef”>
- {{ item }}
- </li>
- </ul>
- </template>
- <script>
- import { ref, nextTick } from ‘vue’;
- export default {
- setup() {
- const arr = ref([1, 2, 3]);
- // 存储dom数组
- const Ref = ref([]);
- const setRef = (el) => {
- Ref.value.push(el);
- };
- nextTick(() => {
- console.dir(Ref.value);
- });
- return {
- arr,
- setRef
- };
- }
- };
- </script>
本文链接地址: vue 3 获取DOM