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

/ 7,189评论 / 37877阅读 / 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. RonnieLit说道:

    http://kamagraes.site/# farmacias online seguras en españa

  2. EddieBug说道:

    farmacias online baratas farmacia 24 horas farmacias online seguras en espaГ±a

  3. MarioRip说道:

    farmacias baratas online envГ­o gratis: Comprar Cialis sin receta – farmacias online seguras en espaГ±a

  4. JasonDer说道:

    http://sildenafilo.store/# comprar viagra online en andorra

  5. MarioRip说道:

    farmacia online madrid: farmacia 24 horas – farmacias online seguras en espaГ±a

  6. RonnieLit说道:

    http://sildenafilo.store/# sildenafilo 50 mg precio sin receta

  7. EddieBug说道:

    sildenafilo 100mg sin receta comprar viagra viagra online gibraltar

  8. JasonDer说道:

    https://tadalafilo.pro/# farmacia online internacional

  9. RonnieLit说道:

    https://farmacia.best/# farmacias online seguras

  10. RonnieLit说道:

    http://sildenafilo.store/# sildenafilo 100mg precio españa

  11. MarioRip说道:

    farmacias baratas online envГ­o gratis: kamagra gel – farmacias online seguras

  12. EddieBug说道:

    farmacias baratas online envГ­o gratis comprar cialis online seguro opiniones farmacia online barata

  13. JasonDer说道:

    https://farmacia.best/# farmacia online internacional

  14. RonnieLit说道:

    https://sildenafilo.store/# sildenafilo 50 mg precio sin receta

  15. RonnieLit说道:

    https://sildenafilo.store/# viagra online cerca de bilbao

  16. MarioRip说道:

    farmacias online baratas: gran farmacia online – farmacia online internacional

  17. JasonDer说道:

    http://tadalafilo.pro/# п»їfarmacia online

  18. MarioRip说道:

    comprar viagra online en andorra: comprar viagra – comprar viagra en espaГ±a amazon

  19. RonnieLit说道:

    https://vardenafilo.icu/# farmacia online envío gratis

  20. RonnieLit说道:

    https://tadalafilo.pro/# farmacia online internacional

  21. RonnieLit说道:

    http://kamagraes.site/# farmacias online baratas

  22. RonnieLit说道:

    http://farmacia.best/# farmacias online baratas

  23. RonnieLit说道:

    http://sildenafilo.store/# sildenafilo precio farmacia

  24. JasonDer说道:

    http://tadalafilo.pro/# farmacia online madrid

  25. MarioRip说道:

    farmacias baratas online envГ­o gratis: comprar kamagra – п»їfarmacia online

  26. EddieBug说道:

    farmacia online envГ­o gratis Precio Levitra En Farmacia farmacia online envГ­o gratis

  27. RonnieLit说道:

    http://tadalafilo.pro/# farmacias online seguras

  28. RonnieLit说道:

    http://tadalafilo.pro/# farmacia barata

  29. RonnieLit说道:

    http://kamagraes.site/# farmacia online

  30. JasonDer说道:

    http://tadalafilo.pro/# farmacia online barata

  31. MarioRip说道:

    farmacias online baratas: tadalafilo – farmacias online baratas

  32. JasonDer说道:

    http://vardenafilo.icu/# farmacia online barata

  33. MarioRip说道:

    farmacia envГ­os internacionales: Comprar Levitra Sin Receta En Espana – farmacia online barata

  34. RonnieLit说道:

    http://farmacia.best/# farmacias online baratas

  35. EddieBug说道:

    farmacias online seguras en espaГ±a tadalafilo farmacias baratas online envГ­o gratis

  36. RonnieLit说道:

    http://kamagraes.site/# farmacia envíos internacionales

  37. RonnieLit说道:

    https://vardenafilo.icu/# farmacias online seguras en españa

  38. RonnieLit说道:

    https://farmacia.best/# farmacia online 24 horas

  39. MarioRip说道:

    farmacias online seguras: Precio Levitra En Farmacia – farmacia barata

  40. JasonDer说道:

    https://tadalafilo.pro/# п»їfarmacia online

  41. RonnieLit说道:

    http://kamagraes.site/# farmacia barata

  42. RonnieLit说道:

    http://vardenafilo.icu/# farmacia online internacional

  43. EddieBug说道:

    farmacia online 24 horas farmacia online 24 horas farmacia 24h

  44. RonnieLit说道:

    https://tadalafilo.pro/# farmacias online seguras

  45. MarioRip说道:

    farmacia online 24 horas: vardenafilo sin receta – farmacias online seguras en espaГ±a

  46. JasonDer说道:

    https://farmacia.best/# farmacia barata

  47. RonnieLit说道:

    https://farmacia.best/# farmacia envíos internacionales

  48. TracyFoome说道:

    farmacie online affidabili: kamagra gel – comprare farmaci online con ricetta

发表回复

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