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

/ 2,719评论 / 14244阅读 / 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. Michaelsew说道:

    http://canadaph24.pro/# canadian pharmacy online

  2. Marcelcrolf说道:

    http://indiaph24.store/# top 10 online pharmacy in india

  3. Pedropaite说道:

    п»їbest mexican online pharmacies: Mexican Pharmacy Online – mexican pharmaceuticals online

  4. RickyAlage说道:

    mexico pharmacies prescription drugs Mexican Pharmacy Online medicine in mexico pharmacies

  5. Marcelcrolf说道:

    http://indiaph24.store/# reputable indian pharmacies

  6. Michaelsew说道:

    https://indiaph24.store/# reputable indian pharmacies

  7. StevenSen说道:

    india pharmacy mail order: indian pharmacy fast delivery – cheapest online pharmacy india

  8. Marcelcrolf说道:

    https://mexicoph24.life/# mexican mail order pharmacies

  9. RickyAlage说道:

    rate canadian pharmacies Licensed Canadian Pharmacy trustworthy canadian pharmacy

  10. Marcelcrolf说道:

    https://indiaph24.store/# mail order pharmacy india

  11. RickyAlage说道:

    buying prescription drugs in mexico online cheapest mexico drugs buying prescription drugs in mexico

  12. Marcelcrolf说道:

    https://indiaph24.store/# india pharmacy

  13. RickyAlage说道:

    cheapest online pharmacy india indian pharmacy cheapest online pharmacy india

  14. Michaelsew说道:

    https://canadaph24.pro/# canadian pharmacy online store

  15. RickyAlage说道:

    mexican mail order pharmacies mexico pharmacy medicine in mexico pharmacies

  16. StevenSen说道:

    pharmacies in mexico that ship to usa: cheapest mexico drugs – pharmacies in mexico that ship to usa

  17. Marcelcrolf说道:

    http://mexicoph24.life/# buying prescription drugs in mexico online

  18. RickyAlage说道:

    п»їbest mexican online pharmacies cheapest mexico drugs п»їbest mexican online pharmacies

  19. Marcelcrolf说道:

    http://indiaph24.store/# cheapest online pharmacy india

  20. RickyAlage说道:

    Online medicine home delivery Generic Medicine India to USA india pharmacy

  21. Marcelcrolf说道:

    http://indiaph24.store/# indian pharmacy

  22. Michaelsew说道:

    http://canadaph24.pro/# canadian pharmacy checker

  23. RickyAlage说道:

    buying from online mexican pharmacy Mexican Pharmacy Online medication from mexico pharmacy

  24. Marcelcrolf说道:

    http://canadaph24.pro/# best online canadian pharmacy

  25. StevenSen说道:

    mexican rx online: cheapest mexico drugs – buying from online mexican pharmacy

  26. RickyAlage说道:

    buying prescription drugs in mexico online Online Pharmacies in Mexico mexican pharmacy

  27. Marcelcrolf说道:

    http://canadaph24.pro/# canadian pharmacy ltd

  28. RickyAlage说道:

    india pharmacy mail order indian pharmacy fast delivery indian pharmacy paypal

  29. Marcelcrolf说道:

    http://mexicoph24.life/# mexican rx online

  30. Michaelsew说道:

    http://indiaph24.store/# indian pharmacy online

  31. Marcelcrolf说道:

    https://canadaph24.pro/# northwest pharmacy canada

  32. RickyAlage说道:

    medication from mexico pharmacy Online Pharmacies in Mexico mexican border pharmacies shipping to usa

  33. JustinTut说道:

    cheapest online pharmacy india http://indiaph24.store/# indian pharmacy
    india online pharmacy

  34. Marcelcrolf说道:

    http://canadaph24.pro/# ed meds online canada

  35. RickyAlage说道:

    mexico drug stores pharmacies cheapest mexico drugs medicine in mexico pharmacies

  36. StevenSen说道:

    canadian drug pharmacy: canadian pharmacies – canada online pharmacy

  37. Marcelcrolf说道:

    http://mexicoph24.life/# best online pharmacies in mexico

  38. RickyAlage说道:

    mexican border pharmacies shipping to usa Online Pharmacies in Mexico mexican pharmacy

  39. Michaelsew说道:

    http://canadaph24.pro/# safe canadian pharmacies

  40. Marcelcrolf说道:

    http://indiaph24.store/# indianpharmacy com

  41. RickyAlage说道:

    purple pharmacy mexico price list cheapest mexico drugs best online pharmacies in mexico

  42. Marcelcrolf说道:

    https://canadaph24.pro/# canadian pharmacy tampa

回复 נערות ליווי 取消回复

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