Vue ref atributi


Misol

<p> tegidagi matnni o‘zgartirish uchun ref atributidan foydalanish:

<div id="app">
  <p ref="pEl">Initial text.</p>
  <button v-on:click="changeText">Change text</button>
</div>

<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script type="module">
  const app = Vue.createApp({
    methods: {
      changeText(){
        this.$refs.pEl.innerHTML = "Hello!";
      }
    }
  })
  app.mount('#app')
</script>
O‘zingiz sinab ko‘ring »

Quyida ko‘proq misollarni ko‘ring.


ULASHISH

Ta’rif va qo‘llanilishi

ref atributi <template> ichidagi elementlarni belgilash uchun ishlatiladi, shunda ularga <script> ichidagi $refs obyekti orqali murojaat qilish mumkin bo‘ladi.

Oddiy JavaScript’dagi getElementById() yoki querySelector() kabi metodlarga muqobil sifatida Vue’dagi ref atributi va $refs obyektidan foydalanishimiz mumkin.

Agar v-for bilan yaratilgan HTML elementlari ref atributiga ega bo‘lsa, hosil bo‘lgan DOM elementlari ushbu misolda ko‘rsatilganidek $refs obyektiga massiv sifatida qo‘shiladi:

Misol

<ul>
  <li v-for="x in liTexts" ref="liEl">{{ x }}</li>
</ul>
Misolni ishga tushirish »

Ko‘proq misollar

1-misol

<p> elementi ichidagi matn o‘zgartiriladi.

<template>
  <h1>Example</h1>
  <p>Click the button to put "Hello!" as the text in the green p element.</p>
  <button @click="changeVal">Change Text</button><br>
  <p ref="pEl" id="pEl">This is the initial text</p>
</template>

<script>
  export default {
    methods: {
      changeVal() { 
        this.$refs.pEl.innerHTML = "Hello!";
      }
    }
  };
</script>

<style>
#pEl {
  background-color: lightgreen;
  display: inline-block;
}
</style>
Misolni ishga tushirish »

2-misol

Birinchi <p> tegidagi matn ikkinchi <p> tegiga ko‘chiriladi.

<template>
  <h1>Example</h1>
  <p ref="p1">Click the button to copy this text into the paragraph below.</p>
  <button @click="transferText">Transfer text</button>
  <p ref="p2">...</p>
</template>

<script>
  export default {
    methods: {
      transferText() { 
        this.$refs.p2.innerHTML = this.$refs.p1.innerHTML;
      }
    }
  };
</script>
Misolni ishga tushirish »

3-misol

<p> elementi input maydoniga yozilayotgan matn bilan bir xil tarkibni oladi.

<template>
  <h1>Example</h1>
  <p>Start writing inside the input element, and the text will be copied into the last paragraph by the use of the '$refs' object.</p>
  <input ref="inputEl" @input="getRefs" placeholder="Write something..">
  <p ref="pEl"></p>
</template>

<script>
  export default {
    methods: {
      getRefs() { 
        this.$refs.pEl.innerHTML = this.$refs.inputEl.value;
      }
    }
  };
</script>
Misolni ishga tushirish »

4-misol

Tugma $refs obyekti ichida massiv elementi sifatida saqlangan uchinchi ro‘yxat elementini ko‘rsatadi.

<template>
  <h1>Example</h1>
  <p>Click the button to reveal the 3rd list element stored as an array element in the $refs object.</p>
  <button @click="getValue">Get the 3rd list element</button><br>
  <ul>
    <li v-for="x in liTexts" ref="liEl">{{ x }}</li>
  </ul>
  <pre>{{ thirdEl }}</pre>
</template>

<script>
  export default {
    data() {
      return {
        thirdEl: ' ',
        liTexts: ['Apple','Banana','Kiwi','Tomato','Lichi']
      }
    },
    methods: {
      getValue() { 
        this.thirdEl = this.$refs.liEl[2].innerHTML;
        console.log("this.$refs.liEl = ",this.$refs.liEl);
      }
    }
  };
</script>

<style>
pre {
  background-color: lightgreen;
  display: inline-block;
}
</style>
Misolni ishga tushirish »


Tegishli sahifalar

Vue darsligi: Vue shablon reflari (template refs)

Vue darsligi: Vue v-for direktivasi

Vue ma’lumotnomasi: Vue $refs obyekti


W3Schools Pathfinder

Yutuqlaringizni kuzating – bu bepul!