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

/ 3,116评论 / 18898阅读 / 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. Terryflodo说道:

    viagra pfizer 25mg prezzo sildenafil 100mg prezzo cialis farmacia senza ricetta

  2. TracyFoome说道:

    alternativa al viagra senza ricetta in farmacia: viagra online siti sicuri – viagra online consegna rapida

  3. SidneyFub说道:

    http://kamagrait.club/# acquistare farmaci senza ricetta

  4. TracyFoome说道:

    comprare farmaci online con ricetta: avanafil – acquistare farmaci senza ricetta

  5. TracyFoome说道:

    farmacia online: kamagra oral jelly consegna 24 ore – farmacie on line spedizione gratuita

  6. Sonnynap说道:

    gel per erezione in farmacia: alternativa al viagra senza ricetta in farmacia – viagra naturale in farmacia senza ricetta

  7. TracyFoome说道:

    farmacia online migliore: kamagra gel prezzo – farmacia online

  8. Terryflodo说道:

    farmacie online autorizzate elenco kamagra gel prezzo farmacia online migliore

  9. TracyFoome说道:

    farmacie on line spedizione gratuita: Dove acquistare Cialis online sicuro – farmacia online migliore

  10. SidneyFub说道:

    https://sildenafilit.bid/# viagra online consegna rapida

  11. TracyFoome说道:

    top farmacia online: avanafil prezzo in farmacia – farmacia online più conveniente

  12. Terryflodo说道:

    acquistare farmaci senza ricetta farmacia online miglior prezzo farmacie online autorizzate elenco

  13. TracyFoome说道:

    farmacie online sicure: avanafil prezzo in farmacia – farmacie online sicure

  14. Sonnynap说道:

    farmacia online: Farmacie che vendono Cialis senza ricetta – farmacie on line spedizione gratuita

  15. TracyFoome说道:

    comprare farmaci online all’estero: farmacia online spedizione gratuita – farmacia online migliore

  16. SidneyFub说道:

    http://sildenafilit.bid/# cialis farmacia senza ricetta

  17. TracyFoome说道:

    farmacie online affidabili: dove acquistare cialis online sicuro – farmacie online sicure

  18. Terryflodo说道:

    farmacia online piГ№ conveniente kamagra farmacie online affidabili

  19. TracyFoome说道:

    comprare farmaci online con ricetta: kamagra gold – farmacia online migliore

  20. Sonnynap说道:

    farmacia online miglior prezzo: kamagra gel – farmacia online migliore

  21. TracyFoome说道:

    farmacia online più conveniente: avanafil generico prezzo – farmacie on line spedizione gratuita

  22. TracyFoome说道:

    viagra naturale: viagra prezzo – viagra online in 2 giorni

  23. SidneyFub说道:

    https://tadalafilit.store/# comprare farmaci online con ricetta

  24. Terryflodo说道:

    farmacie online autorizzate elenco kamagra oral jelly consegna 24 ore farmacie online affidabili

  25. TracyFoome说道:

    migliori farmacie online 2023: kamagra gold – farmacia online senza ricetta

  26. Sonnynap说道:

    farmacie online autorizzate elenco: farmacia online miglior prezzo – acquisto farmaci con ricetta

  27. TracyFoome说道:

    acquistare farmaci senza ricetta: farmacie online affidabili – farmacie online sicure

  28. TracyFoome说道:

    farmacia online: kamagra oral jelly – farmacia online più conveniente

  29. Terryflodo说道:

    viagra online spedizione gratuita sildenafil 100mg prezzo kamagra senza ricetta in farmacia

  30. SidneyFub说道:

    http://farmaciait.pro/# farmaci senza ricetta elenco

  31. TracyFoome说道:

    viagra 100 mg prezzo in farmacia: sildenafil prezzo – pillole per erezione in farmacia senza ricetta

  32. TracyFoome说道:

    farmacia online miglior prezzo: Farmacie a roma che vendono cialis senza ricetta – comprare farmaci online con ricetta

  33. Sonnynap说道:

    farmacie online autorizzate elenco: farmacia online miglior prezzo – farmacie online affidabili

  34. TracyFoome说道:

    farmacie online affidabili: cialis generico – farmacia online miglior prezzo

  35. TracyFoome说道:

    farmacia online più conveniente: kamagra gel prezzo – acquisto farmaci con ricetta

  36. Terryflodo说道:

    п»їfarmacia online migliore kamagra gold comprare farmaci online con ricetta

  37. SidneyFub说道:

    http://sildenafilit.bid/# viagra naturale in farmacia senza ricetta

  38. Michaelgew说道:

    can i purchase generic clomid pills: Clomiphene Citrate 50 Mg – where to get generic clomid pill

  39. MichaelDaype说道:

    https://paxlovid.club/# buy paxlovid online

  40. Michaelgew说道:

    neurontin 100mg discount: generic gabapentin – prescription price for neurontin

  41. MichaelDaype说道:

    https://gabapentin.life/# neurontin 400 mg tablets

  42. MichaelDaype说道:

    http://claritin.icu/# ventolin hfa inhaler

  43. Michaelgew说道:

    wellbutrin 30 mg: Wellbutrin online with insurance – best generic wellbutrin

  44. MichaelDaype说道:

    http://paxlovid.club/# Paxlovid buy online

  45. Michaelgew说道:

    buy paxlovid online: cheap paxlovid online – buy paxlovid online

  46. MathewPog说道:

    paxlovid generic https://paxlovid.club/# paxlovid india

  47. MichaelDaype说道:

    https://gabapentin.life/# neurontin 600

  48. Michaelgew说道:

    wellbutrin order online: Buy bupropion online Europe – wellbutrin online india

  49. MichaelDaype说道:

    https://wellbutrin.rest/# 2018 wellbutrin

  50. Michaelgew说道:

    buy generic clomid without insurance: Clomiphene Citrate 50 Mg – where can i buy clomid without a prescription

发表回复

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