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

/ 7,209评论 / 37912阅读 / 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. MarvinMek说道:

    buy zithromax 500mg online zithromax prescription buy generic zithromax online

  2. Billyeroge说道:

    https://prednisoned.online/# 25 mg prednisone

  3. MarvinMek说道:

    prednisone brand name us prednisone coupon prednisone tabs 20 mg

  4. HenryIsona说道:

    generic amoxicillin 500mg: buy amoxicillin 500mg – amoxicillin 500 mg cost

  5. Richardfrota说道:

    doxycycline 50 mg: doxycycline 500mg – doxycycline hyclate 100 mg cap

  6. HenryIsona说道:

    generic gabapentin: buy generic neurontin – medication neurontin 300 mg

  7. Billyeroge说道:

    http://prednisoned.online/# prednisone buy no prescription

  8. MarvinMek说道:

    amoxicillin pharmacy price 875 mg amoxicillin cost over the counter amoxicillin canada

  9. Billyeroge说道:

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

  10. CharlesGib说道:

    doxycycline hyc 100mg: doxycycline 100mg online – buy doxycycline without prescription uk

  11. CharlesGib说道:

    buy cheap zithromax online: generic zithromax 500mg india – order zithromax without prescription

  12. Richardfrota说道:

    neurontin 300 mg buy: neurontin 800 mg pill – neurontin prescription cost

  13. HenryIsona说道:

    how to get zithromax online: zithromax 500 tablet – zithromax prescription online

  14. MarvinMek说道:

    doxycycline order online where to get doxycycline buy doxycycline without prescription

  15. HenryIsona说道:

    neurontin prices generic: neurontin 600 mg capsule – purchase neurontin canada

  16. Billyeroge说道:

    http://amoxila.pro/# where can i buy amoxicillin over the counter

  17. MarvinMek说道:

    doxycycline doxycycline mono doxycycline 100mg price

  18. Billyeroge说道:

    http://prednisoned.online/# prednisone without prescription medication

  19. HenryIsona说道:

    prednisone 10 mg price: prednisone 5mg over the counter – prednisone 5 50mg tablet price

  20. shanon ho说道:

    Way cool! Some very valid points! I appreciate you penning this
    article and the rest of the website is very good.

  21. MarvinMek说道:

    otc prednisone cream prednisone pills cost prednisone uk

  22. HenryIsona说道:

    generic amoxicillin: amoxicillin price canada – buy amoxicillin online with paypal

  23. Billyeroge说道:

    http://zithromaxa.store/# buy zithromax

  24. MarvinMek说道:

    neurontin 300 mg cap neurontin over the counter medicine neurontin

  25. Billyeroge说道:

    https://amoxila.pro/# where to buy amoxicillin 500mg without prescription

  26. Richardfrota说道:

    zithromax for sale cheap: zithromax for sale online – generic zithromax 500mg india

  27. These are actually fantastic ideas in on the topic
    of blogging. You have touched some nice points here. Any way keep up wrinting.

  28. HenryIsona说道:

    prednisone 20 mg generic: prednisone 10mg prices – buying prednisone from canada

  29. MarvinMek说道:

    where to get doxycycline doxycycline medication odering doxycycline

  30. HenryIsona说道:

    generic zithromax over the counter: zithromax cost – zithromax z-pak price without insurance

  31. CharlesGib说道:

    zithromax price canada: purchase zithromax z-pak – zithromax buy online

  32. Billyeroge说道:

    http://amoxila.pro/# generic for amoxicillin

  33. CharlesGib说道:

    buying amoxicillin online: can i buy amoxicillin over the counter in australia – can i buy amoxicillin over the counter in australia

  34. MarvinMek说道:

    prednisone cream over the counter prednisone pill 10 mg buy prednisone 50 mg

  35. Richardfrota说道:

    buy zithromax canada: where can i buy zithromax capsules – buy zithromax online with mastercard

  36. Billyeroge说道:

    https://prednisoned.online/# prednisone 20 mg pill

  37. HenryIsona说道:

    prednisone 0.5 mg: cost of prednisone 5mg tablets – cheapest prednisone no prescription

  38. MarvinMek说道:

    zithromax buy online no prescription can you buy zithromax over the counter in canada zithromax price south africa

  39. HenryIsona说道:

    doxycycline prices: doxycycline 200 mg – doxycycline 100 mg

  40. Billyeroge说道:

    http://amoxila.pro/# amoxicillin capsule 500mg price

  41. HenryIsona说道:

    doxycycline online: doxycycline 50 mg – buy cheap doxycycline online

  42. MarvinMek说道:

    how much is neurontin pills medicine neurontin capsules cost of neurontin 600 mg

  43. HenryIsona说道:

    doxy: buy generic doxycycline – purchase doxycycline online

  44. Richardfrota说道:

    order amoxicillin online no prescription: amoxicillin 250 mg capsule – generic amoxicillin over the counter

  45. Billyeroge说道:

    https://amoxila.pro/# amoxicillin 500mg cost

  46. MarvinMek说道:

    prednisone 50 mg price online order prednisone 10mg prednisone uk over the counter

  47. Billyeroge说道:

    http://amoxila.pro/# can we buy amoxcillin 500mg on ebay without prescription

  48. CharlesGib说道:

    cheap prednisone 20 mg: 5 mg prednisone daily – apo prednisone

发表回复

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