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

/ 3,200评论 / 19251阅读 / 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. JamesSew说道:

    п»їcytotec pills online: buy cytotec pills online cheap – buy cytotec in usa

  2. JeromedUb说道:

    buy cipro online without prescription cipro 500mg best prices buy generic ciprofloxacin

  3. JeromedUb说道:

    buy cipro cheap ciprofloxacin mail online where can i buy cipro online

  4. RussellVeila说道:

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

  5. JamesSew说道:

    diflucan order online uk: generic diflucan prices – buy diflucan 150 mg

  6. JeromedUb说道:

    Abortion pills online п»їcytotec pills online cytotec online

  7. JeromedUb说道:

    buy cipro online without prescription antibiotics cipro cipro for sale

  8. RussellVeila说道:

    cipro 500mg best prices: ciprofloxacin mail online – ciprofloxacin

  9. Archierut说道:

    https://nolvadex.icu/# tamoxifen and depression

  10. Williamzew说道:

    where to get doxycycline: doxycycline monohydrate – doxycycline tetracycline

  11. JeromedUb说道:

    cipro ciprofloxacin purchase cipro buy generic ciprofloxacin

  12. JeromedUb说道:

    tamoxifen lawsuit nolvadex steroids how to lose weight on tamoxifen

  13. Archierut说道:

    http://misoprostol.top/# buy cytotec over the counter

  14. RussellVeila说道:

    nolvadex gynecomastia: tamoxifen generic – does tamoxifen cause joint pain

  15. JeromedUb说道:

    can you buy diflucan without a prescription where can i get diflucan buy diflucan otc

  16. JeromedUb说道:

    tamoxifen joint pain tamoxifen for sale nolvadex for sale

  17. JamesSew说道:

    liquid tamoxifen: nolvadex 10mg – tamoxifen generic

  18. RussellVeila说道:

    order cytotec online: buy misoprostol over the counter – buy cytotec online

  19. Williamzew说道:

    diflucan price in india: where can i get diflucan – diflucan 150 mg over the counter

  20. JamesSew说道:

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

  21. RussellVeila说道:

    Abortion pills online: buy misoprostol over the counter – cytotec pills online

  22. JeromedUb说道:

    cipro for sale ciprofloxacin generic buy cipro cheap

  23. JeromedUb说道:

    cipro for sale п»їcipro generic ciprofloxacin 500 mg tablet price

  24. RussellVeila说道:

    diflucan 150 tablet: can you buy diflucan over the counter in usa – diflucan online nz

  25. JeromedUb说道:

    tamoxifen and grapefruit tamoxifen medication tamoxifen warning

  26. JeromedUb说道:

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

  27. Williamzew说道:

    what happens when you stop taking tamoxifen: is nolvadex legal – nolvadex for sale

  28. Archierut说道:

    http://diflucan.icu/# diflucan 6 tabs

  29. RussellVeila说道:

    doxycycline 100mg online: order doxycycline 100mg without prescription – doxy

  30. JeromedUb说道:

    fluconazole diflucan medication diflucan price medicine diflucan price

  31. JeromedUb说道:

    doxycycline 100mg price doxycycline hyclate 100 mg cap buy doxycycline online 270 tabs

  32. RussellVeila说道:

    doxycycline prices: doxycycline online – doxycycline order online

  33. RussellVeila说道:

    tamoxifen chemo: nolvadex half life – alternative to tamoxifen

  34. JeromedUb说道:

    diflucan 200 where can i get diflucan online generic diflucan fluconazole

  35. JeromedUb说道:

    diflucan 100 mg diflucan tablets buy online no script diflucan online cheap

  36. JamesSew说道:

    ciprofloxacin generic: cipro pharmacy – ciprofloxacin generic price

  37. Williamzew说道:

    cipro online no prescription in the usa: buy cipro – where can i buy cipro online

  38. RussellVeila说道:

    doxycycline: doxy – doxycycline

  39. Archierut说道:

    http://misoprostol.top/# п»їcytotec pills online

  40. JeromedUb说道:

    how to buy doxycycline online doxycycline 150 mg doxycycline vibramycin

  41. JeromedUb说道:

    diflucan tablets where to purchase over the counter diflucan pill diflucan 150 mg over the counter

  42. JamesSew说道:

    buy diflucan without prescription: diflucan generic brand – diflucan over the counter nz

  43. Archierut说道:

    https://ciprofloxacin.guru/# buy cipro online canada

  44. RussellVeila说道:

    ciprofloxacin: buy cipro cheap – cipro generic

  45. Williamzew说道:

    buy cheap doxycycline: doxycycline order online – how to order doxycycline

  46. JeromedUb说道:

    effexor and tamoxifen tamoxifen cost tamoxifen dose

  47. JeromedUb说道:

    doxycycline hydrochloride 100mg doxycycline 100mg doxycycline 100mg capsules

  48. JamesSew说道:

    tamoxifen buy: aromatase inhibitors tamoxifen – raloxifene vs tamoxifen

  49. RussellVeila说道:

    buy cipro online without prescription: ciprofloxacin 500mg buy online – buy generic ciprofloxacin

发表回复

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