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

/ 7,222评论 / 37927阅读 / 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. Davidwen说道:

    best ed pills at gnc ed treatment pills – pills erectile dysfunction edpills.tech

  2. JeffreyTic说道:

    best ed drugs compare ed drugs ed medications edpills.tech

  3. RalphDug说道:

    https://edpills.tech/# male ed pills edpills.tech

  4. RalphDug说道:

    https://canadiandrugs.tech/# canadian pharmacy world canadiandrugs.tech

  5. RalphDug说道:

    https://edpills.tech/# best pill for ed edpills.tech

  6. Wilfredbag说道:

    canadian online drugs canadian pharmacy 365 – buy prescription drugs from canada cheap canadiandrugs.tech

  7. RalphDug说道:

    https://edpills.tech/# top erection pills edpills.tech

  8. JeffreyTic说道:

    reliable canadian pharmacy canadian pharmacy 365 canadian discount pharmacy canadiandrugs.tech

  9. Davidwen说道:

    canadian pharmacies that deliver to the us canadian pharmacy mall – canadian family pharmacy canadiandrugs.tech

  10. Joshuagap说道:

    https://canadapharmacy.guru/# canadian online drugstore canadapharmacy.guru

  11. RalphDug说道:

    http://indiapharmacy.guru/# india pharmacy indiapharmacy.guru

  12. Wilfredbag说道:

    canadian pharmacy drugs online canadian pharmacy com – legit canadian pharmacy canadiandrugs.tech

  13. RalphDug说道:

    https://edpills.tech/# best ed drug edpills.tech

  14. RalphDug说道:

    https://indiapharmacy.guru/# buy prescription drugs from india indiapharmacy.guru

  15. JeffreyTic说道:

    canadian pharmacies comparison canadian pharmacy ratings canadian pharmacy store canadiandrugs.tech

  16. RalphDug说道:

    http://canadiandrugs.tech/# precription drugs from canada canadiandrugs.tech

  17. Davidwen说道:

    legitimate canadian mail order pharmacy safe canadian pharmacy – drugs from canada canadiandrugs.tech

  18. RalphDug说道:

    https://canadiandrugs.tech/# buy prescription drugs from canada cheap canadiandrugs.tech

  19. RalphDug说道:

    http://canadiandrugs.tech/# canadian pharmacy ed medications canadiandrugs.tech

  20. RalphDug说道:

    http://indiapharmacy.guru/# indian pharmacies safe indiapharmacy.guru

  21. Wilfredbag说道:

    ed medication online erectile dysfunction drug – best ed medications edpills.tech

  22. RalphDug说道:

    https://canadiandrugs.tech/# prescription drugs canada buy online canadiandrugs.tech

  23. JeffreyTic说道:

    canadian medications canadian mail order pharmacy online canadian drugstore canadiandrugs.tech

  24. RalphDug说道:

    http://indiapharmacy.guru/# legitimate online pharmacies india indiapharmacy.guru

  25. Joshuagap说道:

    http://mexicanpharmacy.company/# mexican pharmacy mexicanpharmacy.company

  26. Davidwen说道:

    canadian neighbor pharmacy canada pharmacy world – legal to buy prescription drugs from canada canadiandrugs.tech

  27. RalphDug说道:

    https://indiapharmacy.guru/# best online pharmacy india indiapharmacy.guru

  28. RalphDug说道:

    https://edpills.tech/# men’s ed pills edpills.tech

  29. JeffreyTic说道:

    reputable indian pharmacies reputable indian online pharmacy india pharmacy mail order indiapharmacy.guru

  30. Wilfredbag说道:

    the best ed pill cure ed – cheap erectile dysfunction pill edpills.tech

  31. Davidwen说道:

    india pharmacy top 10 online pharmacy in india – п»їlegitimate online pharmacies india indiapharmacy.guru

  32. Joshuagap说道:

    http://canadapharmacy.guru/# thecanadianpharmacy canadapharmacy.guru

  33. Scottfrurn说道:

    п»їbest mexican online pharmacies buying prescription drugs in mexico mexican online pharmacies prescription drugs

  34. RaymondLiata说道:

    pharmacies in mexico that ship to usa mexican drugstore online pharmacies in mexico that ship to usa

  35. Coreynuh说道:

    http://mexicanpharmacy.cheap/# buying from online mexican pharmacy

  36. RaymondLiata说道:

    mexico drug stores pharmacies mexican mail order pharmacies purple pharmacy mexico price list

  37. RaymondLiata说道:

    medicine in mexico pharmacies buying prescription drugs in mexico online buying prescription drugs in mexico

  38. Michaelmub说道:

    https://mexicanpharmacy.cheap/# best online pharmacies in mexico

  39. RaymondLiata说道:

    mexican pharmacy mexican pharmacy medicine in mexico pharmacies

  40. Scottfrurn说道:

    mexican rx online mexican border pharmacies shipping to usa purple pharmacy mexico price list

  41. RaymondLiata说道:

    best online pharmacies in mexico mexican online pharmacies prescription drugs mexico drug stores pharmacies

  42. Coreynuh说道:

    http://mexicanpharmacy.cheap/# mexican border pharmacies shipping to usa

  43. RaymondLiata说道:

    purple pharmacy mexico price list mexican rx online medicine in mexico pharmacies

  44. Normancah说道:

    medication from mexico pharmacy buying from online mexican pharmacy reputable mexican pharmacies online

  45. Scottfrurn说道:

    mexican border pharmacies shipping to usa medication from mexico pharmacy п»їbest mexican online pharmacies

  46. Coreynuh说道:

    http://mexicanpharmacy.cheap/# medicine in mexico pharmacies

  47. Michaelmub说道:

    https://mexicanpharmacy.cheap/# purple pharmacy mexico price list

  48. RaymondLiata说道:

    medicine in mexico pharmacies best mexican online pharmacies best mexican online pharmacies

  49. Scottfrurn说道:

    п»їbest mexican online pharmacies mexico drug stores pharmacies reputable mexican pharmacies online

  50. Coreynuh说道:

    https://mexicanpharmacy.cheap/# buying from online mexican pharmacy

发表回复

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