[vue3]组件操作Dom元素,多个同名ref时的解决方法,返回了proxy对象时的使用

/ 7,196评论 / 37886阅读 / 5点赞

1. 原生js中我们会使用document.getElementsByClassName(),document.getElementById()等获取dom元素,但在vue中,更推荐使用ref获取。

2. 不同文件的ref相互独立,即使同名也不会互相影响而导致获取错误。一个组件被多次引用后同时存在多个实例时,每个实例的ref也是互相独立的。这一点显然比getElementById()要好很多。

3. 标签的ref属性值在每一个vue文件中需要是唯一的,否则可能在获取时发生与预期不同的效果。显然使用v-for时如果单项带有ref就需要我们解决这个问题。

使用ref绑定Dom元素

<template>
    <span id="myspanid" ref="mySpanRef">hello coolight</span>
</template>

获取

获取的方式很多,这里介绍其中的几种,以及提及一些document的方法和注意事项

祖传getElementById()

<script setup>
import { onMounted } from "vue";

let span_id = document.getElementById("myspanid");
console.log("setup: span_id = ", span_id);

onMounted(() => {
    console.log("onMounted: span_id = ", span_id);
    span_id = document.getElementById("myspanid");
    console.log("onMounted: span_id = ", span_id);
})
</script>

<template>
    <span id="myspanid" ref="mySpanRef">hello coolight</span>
</template>

ref(null)

<script setup>
import { ref,onMounted, getCurrentInstance } from "vue";

let mySpanRef = ref(null);
console.log("setup: mySpanRef = ", mySpanRef);
console.log("setup: mySpanRef.value = ", mySpanRef.value);

onMounted(() => {
    console.log("读取setup获取的mySpanRef:");
    console.log("onMounted: mySpanRef = ", mySpanRef);
    console.log("onMounted: mySpanRef.value = ", mySpanRef.value);
    mySpanRef = ref(null);
    console.log("读取onMounted获取的mySpanRef:");
    console.log("onMounted: mySpanRef = ", mySpanRef);
    console.log("onMounted: mySpanRef.value = ", mySpanRef.value);
})
</script>

<template>
    <span ref="mySpanRef">hello coolight</span>
</template>
<script setup>
import { ref, onMounted } from "vue";

const mySpanRef = ref(null);

onMounted(() => {
    console.log(mySpanRef);
    console.log(mySpanRef.clientWidth);
    console.log(mySpanRef.value.clientWidth);
})
</script>

<template>
    <span ref="mySpanRef">hello coolight</span>
</template>

$refs.refName

<script setup>
import { onMounted, getCurrentInstance } from "vue";

let mySpan;

onMounted(() => {
    let { $refs } = (getCurrentInstance()).proxy;
    mySpan = $refs.mySpanRef;
    console.log("onMounted: mySpan = ", mySpan);
})
</script>

<template>
    <span ref="mySpanRef">hello coolight</span>
</template>

$refs[refName]

<script setup>
import { onMounted, getCurrentInstance } from "vue";

let mySpan;

onMounted(() => {
    let { $refs } = (getCurrentInstance()).proxy;
    let name = "mySpanRef";
    mySpan = $refs[name]; 
    //mySpan = $refs['mySpanRef'];      //这个也是可以的
    console.log("onMounted: mySpan = ", mySpan);
})
</script>

<template>
    <span ref="mySpanRef">hello coolight</span>
</template>

多个同名ref的解决方法

上面我们都是把ref当成id一样使用,但在v-for后产生的列表项可能遇到ref重复,下面我们聊聊如何解决这个问题

<script setup>
import { ref, onMounted, getCurrentInstance } from "vue";

let mySpanRef = ref(null);

onMounted(() => {
    console.log("ref(null) = ", mySpanRef.value);
    let { $refs } = (getCurrentInstance()).proxy;
    mySpanRef = $refs.mySpanRef;
    console.log("$refs.mySpanRef = ", mySpanRef);
    mySpanRef = $refs['mySpanRef'];
    console.log("$refs['mySpanRef'] = ", mySpanRef);
})
</script>

<template>
    <div>
        <span ref="mySpanRef">hello coolight</span>
        <span ref="mySpanRef">hello 洛天依</span>
    </div>
</template>
<script setup>
import { onMounted, getCurrentInstance } from "vue";

let arr = ['coolight', '洛天依', 'enter', 'shift', 'ctrl', 'Alt', 'ESC'];

onMounted(() => {
    let { $refs } = (getCurrentInstance()).proxy;
    console.log($refs['myspan0']);
    console.log("for:");
    for(let i = arr.length; i-- > 0;) {
        console.log($refs['myspan'+ i][0]);
    }
})
</script>

<template>
    <div style="display:flex;flex-direction: column;">
        <span v-for="(item, index) in arr"
            :ref="'myspan' + index">{{index}}:{{item}}</span>
    </div>
</template>

其他问题

返回的是一个proxy对象

let { $refs } = (getCurrentInstance()).proxy;
let dom = $refs['myul'];    //proxy对象
dom.$el;                    //标签内容
dom.$el.clientWidth;        //通过$el即可同getElementById()获取到的标签一样操作
  1. HenryIsona说道:

    zithromax 500mg: can i buy zithromax over the counter in canada – purchase zithromax z-pak

  2. HenryIsona说道:

    neurontin 800 pill: neurontin generic – neurontin 600mg

  3. CharlesGib说道:

    order prednisone from canada: prednisone cost 10mg – prednisone steroids

  4. MarvinMek说道:

    doxycycline hyc 100mg doxycycline generic doxycycline monohydrate

  5. Richardfrota说道:

    prednisone 10mg cost: prednisone over the counter – price of prednisone tablets

  6. Billyeroge说道:

    https://doxycyclinea.online/# buy doxycycline online

  7. Billyeroge说道:

    http://zithromaxa.store/# zithromax price south africa

  8. HenryIsona说道:

    amoxicillin canada price: amoxicillin 30 capsules price – amoxicillin online pharmacy

  9. MarvinMek说道:

    where can i purchase zithromax online cheap zithromax pills buy cheap zithromax online

  10. HenryIsona说道:

    prednisone 10 mg canada: buy prednisone tablets online – prednisone 10mg buy online

  11. Billyeroge说道:

    https://zithromaxa.store/# zithromax 500 mg for sale

  12. Billyeroge说道:

    http://doxycyclinea.online/# buy doxycycline online uk

  13. Richardfrota说道:

    where can i get doxycycline: doxycycline 100mg capsules – doxycycline 100mg capsules

  14. HenryIsona说道:

    neurontin 30 mg: prescription drug neurontin – neurontin pills

  15. MarvinMek说道:

    prednisone 5 mg tablet cost how to get prednisone tablets by prednisone w not prescription

  16. HenryIsona说道:

    doxycycline 100mg capsules: doxycycline 200 mg – doxycycline 50 mg

  17. CharlesGib说道:

    prednisone 2.5 mg daily: 5 mg prednisone daily – prednisone generic cost

  18. Billyeroge说道:

    https://gabapentinneurontin.pro/# neurontin 100 mg cap

  19. Billyeroge说道:

    https://prednisoned.online/# prednisone no rx

  20. CharlesGib说道:

    amoxicillin buy no prescription: how much is amoxicillin – amoxicillin 500mg pill

  21. Richardfrota说道:

    buy doxycycline online: buy doxycycline hyclate 100mg without a rx – buy doxycycline for dogs

  22. MarvinMek说道:

    zithromax 500mg over the counter generic zithromax 500mg where can i buy zithromax medicine

  23. HenryIsona说道:

    doxycycline prices: doxycycline 100mg dogs – doxycycline 200 mg

  24. HenryIsona说道:

    doxycycline prices: doxycycline vibramycin – doxycycline order online

  25. MarvinMek说道:

    average price of prednisone buy cheap prednisone prednisone 10mg

  26. Billyeroge说道:

    http://amoxila.pro/# amoxicillin online pharmacy

  27. Billyeroge说道:

    http://prednisoned.online/# how to purchase prednisone online

  28. HenryIsona说道:

    doxycycline vibramycin: doxycycline 500mg – 100mg doxycycline

  29. HenryIsona说道:

    can you buy prednisone over the counter in canada: prednisone canada pharmacy – prednisone 200 mg tablets

  30. Richardfrota说道:

    order doxycycline: doxycycline 100mg price – doxycycline generic

  31. HenryIsona说道:

    generic zithromax 500mg india: buy azithromycin zithromax – zithromax 500 tablet

  32. HenryIsona说道:

    prednisone 80 mg daily: prednisone for sale – buying prednisone without prescription

  33. CharlesGib说道:

    amoxicillin 500 mg brand name: amoxicillin capsule 500mg price – amoxicillin buy online canada

  34. CharlesGib说道:

    buy doxycycline online 270 tabs: generic doxycycline – doxycycline medication

  35. Richardfrota说道:

    zithromax 500 mg lowest price drugstore online: can you buy zithromax over the counter in canada – can you buy zithromax over the counter in canada

  36. HenryIsona说道:

    neurontin cream: where can i buy neurontin online – neurontin pills

  37. HenryIsona说道:

    where can i get doxycycline: doxycycline prices – order doxycycline online

  38. MarvinMek说道:

    neurontin 100 mg cost neurontin 2400 mg neurontin cost uk

  39. Richardfrota说道:

    neurontin cap: neurontin capsules 300mg – neurontin prices

  40. MarvinMek说道:

    can i buy zithromax over the counter in canada buy zithromax online fast shipping zithromax buy online

  41. HenryIsona说道:

    buy doxycycline online uk: doxycycline hyc – doxycycline 100mg tablets

  42. CharlesGib说道:

    buy doxycycline 100mg: where to get doxycycline – doxycycline 100mg price

  43. CharlesGib说道:

    neurontin 100 mg: neurontin 400 mg tablets – neurontin 300mg

  44. HenryIsona说道:

    amoxicillin generic: amoxicillin 875 mg tablet – where can i buy amoxicillin without prec

  45. Billyeroge说道:

    http://doxycyclinea.online/# doxycycline generic

  46. Richardfrota说道:

    neurontin 300mg: neurontin 600 mg pill – neurontin over the counter

  47. MarvinMek说道:

    neurontin pills buy neurontin 100 mg neurontin 100mg cost

  48. HenryIsona说道:

    zithromax prescription online: zithromax for sale 500 mg – zithromax 500mg price in india

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注