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

/ 2,931评论 / 18227阅读 / 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. Davidfrify说道:

    tamoxifen estrogen tamoxifen therapy tamoxifen reviews

  2. WilliamRen说道:

    https://cytotec.club/# cytotec abortion pill

  3. WilliamRen说道:

    https://lisinopril.network/# lisinopril 20mg pill

  4. Davidfrify说道:

    Cytotec 200mcg price buy cytotec online buy cytotec over the counter

  5. RandallGuesE说道:

    get cheap propecia no prescription: propecia without prescription – buy generic propecia pill

  6. RonaldSwobe说道:

    https://ciprofloxacin.tech/# antibiotics cipro

  7. RandallGuesE说道:

    ciprofloxacin mail online: cipro for sale – buy generic ciprofloxacin

  8. RonaldSwobe说道:

    https://lisinopril.network/# lisinopril 0.5 mg

  9. WilliamRen说道:

    https://nolvadex.life/# tamoxifen blood clots

  10. Davidfrify说道:

    propecia rx buy cheap propecia buy propecia pills

  11. WilliamRen说道:

    https://cytotec.club/# Misoprostol 200 mg buy online

  12. RandallGuesE说道:

    get propecia tablets: buying propecia without insurance – cost of cheap propecia price

  13. Davidfrify说道:

    tamoxifen vs clomid tamoxifen alternatives premenopausal tamoxifen vs raloxifene

  14. RandallGuesE说道:

    lisinopril pill 20mg: lisinopril online without prescription – cheap lisinopril no prescription

  15. Davidfrify说道:

    ciprofloxacin over the counter buy ciprofloxacin cipro 500mg best prices

  16. WilliamRen说道:

    https://finasteride.store/# cost generic propecia pills

  17. WilliamRen说道:

    http://ciprofloxacin.tech/# buy cipro cheap

  18. RandallGuesE说道:

    cost of generic lisinopril 10 mg: buy lisinopril 10 mg online – price of lisinopril 5mg

  19. RandallGuesE说道:

    tamoxifen citrate pct: tamoxifen cyp2d6 – tamoxifen cyp2d6

  20. Davidfrify说道:

    buy cytotec online fast delivery Cytotec 200mcg price buy cytotec online fast delivery

  21. RonaldSwobe说道:

    https://lisinopril.network/# zestoretic 10 12.5

  22. RonaldSwobe说道:

    https://lisinopril.network/# lisinopril 5 mg tablet price in india

  23. WilliamRen说道:

    https://lisinopril.network/# cost of lisinopril 10 mg

  24. WilliamRen说道:

    http://nolvadex.life/# does tamoxifen cause weight loss

  25. RandallGuesE说道:

    buy cytotec pills: purchase cytotec – Cytotec 200mcg price

  26. Davidfrify说道:

    buy cytotec pills Misoprostol 200 mg buy online buy cytotec online fast delivery

  27. RandallGuesE说道:

    how much is lisinopril 20 mg: lisinopril medicine – no prescription lisinopril

  28. Davidfrify说道:

    buy cheap propecia pills propecia tablets get generic propecia without insurance

  29. RandallGuesE说道:

    cost propecia online: cost generic propecia pills – cost generic propecia online

  30. Davidfrify说道:

    buy cytotec cytotec online buy cytotec online fast delivery

  31. RandallGuesE说道:

    buy misoprostol over the counter: buy cytotec over the counter – cytotec abortion pill

  32. WilliamRen说道:

    https://cytotec.club/# buy misoprostol over the counter

  33. RonaldSwobe说道:

    http://ciprofloxacin.tech/# where can i buy cipro online

  34. WilliamRen说道:

    https://nolvadex.life/# tamoxifen therapy

  35. Davidfrify说道:

    tamoxifen and weight loss arimidex vs tamoxifen bodybuilding tamoxifen hip pain

  36. RonaldSwobe说道:

    https://nolvadex.life/# nolvadex for sale amazon

  37. RandallGuesE说道:

    cheap propecia without prescription: get cheap propecia without insurance – cost of generic propecia without rx

  38. RandallGuesE说道:

    ciprofloxacin: where can i buy cipro online – buy cipro cheap

  39. WilliamRen说道:

    https://lisinopril.network/# lisinopril 10 mg tablet price

  40. Davidfrify说道:

    tamoxifen 20 mg tablet tamoxifen benefits tamoxifen for men

  41. WilliamRen说道:

    http://cytotec.club/# order cytotec online

  42. RandallGuesE说道:

    buy cytotec over the counter: buy cytotec online – Misoprostol 200 mg buy online

  43. RonaldSwobe说道:

    http://nolvadex.life/# clomid nolvadex

  44. RonaldSwobe说道:

    http://lisinopril.network/# lisinopril 20mg pill

  45. Davidfrify说道:

    tamoxifen cost tamoxifen for men where can i buy nolvadex

  46. WilliamRen说道:

    https://finasteride.store/# cost of cheap propecia pill

  47. WilliamRen说道:

    http://cytotec.club/# Abortion pills online

  48. RandallGuesE说道:

    propecia without a prescription: cost generic propecia without dr prescription – cost propecia without prescription

  49. Davidfrify说道:

    ciprofloxacin order online ciprofloxacin over the counter cipro pharmacy

发表回复

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