Vue shablon havolalari (refs)

Vue shablon reflari (template refs) muayyan DOM elementlariga murojaat qilish uchun ishlatiladi.

ref atributi HTML tegiga berilganda, hosil bo‘lgan DOM elementi $refs obyektiga qo‘shiladi.

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

ULASHISH

'ref' atributi va '$refs' obyekti

ref atributiga ega HTML teglari $refs obyektiga qo‘shiladi va ularga keyinroq <script> tegi ichidan kirish mumkin.

Misol

<p> elementi ichidagi matn o‘zgartiriladi.

App.vue:

<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>
  <p ref="pEl">This is the initial text</p>
</template>

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

Quyida bir tegning qiymatini boshqa tegga nusxalash uchun $refs obyektidan foydalaniladigan yana bir misol keltirilgan.

Misol

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

App.vue:

<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 »


$refs orqali input qiymatini olish

$refs obyektiga qo‘shilgan HTML elementining ichiga chuqurroq kirib, uning istalgan xususiyatiga murojaat qilishimiz mumkin.

Misol

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

App.vue:

<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 »

v-for bilan 'ref'

v-for yordamida yaratilgan va ref atributiga ega bo‘lgan HTML elementlari $refs obyektiga massiv sifatida qo‘shiladi.

Misol

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

App.vue:

<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 »

Vue mashqlari

O‘zingizni mashqlar bilan sinab ko‘ring

Mashq:

Reflar ma’lum DOM elementlariga murojaat qilish uchun ishlatiladi.

Ilova DOMga o‘rnatilganda (mounted) ikkinchi

​​tegida "Hello World" ko‘rsatilishi uchun yetishmayotgan kodni kiriting.

<template>
  <p>This is just some text.</p>
  <p >This is the initial text</p>
</template>

<script>
  export default {
    mounted() {
      this..pEl.innerHTML = "Hello World!";
    }
  };
</script>

Mashqni boshlash



W3Schools Pathfinder

Yutuqlaringizni kuzating – bu bepul!