$watch()


Misol

Har safar "value" data xususiyati o‘zgarganda yangi xabar yozadigan kuzatuvchi (watcher) yaratish uchun $watch() metodidan foydalanish.

mounted() {
  this.$watch('value', function() {
    this.results.push('$watch() method')
  })
}
Misolni ishga tushirish »

Quyida ko‘proq misollarni ko‘ring.


ULASHISH

Ta’rif va qo‘llanilishi

$watch() metodi kuzatuvchilar (watchers) yaratish uchun ishlatiladi.

$watch() metodi kuzatuvchini to‘xtatishda foydalanish mumkin bo‘lgan to‘xtatish (stop) funksiyasini qaytaradi. (4-misolga qarang)

Kuzatuvchi qiymatdagi o‘zgarishlarni kuzatish (birinchi argument) va o‘zgarish sodir bo‘lganda nimadir qilish (ikkinchi argument) uchun o‘rnatiladi. Kuzatuvchi uchun boshqa xususiyatlarni ham aniqlash mumkin (uchinchi argument).

Argument Tavsif
kuzatish manbai (watch source) Majburiy. Birinchi argument — kuzatish manbai (watch source). U komponent xususiyati nomini bildiruvchi string (yuqoridagi misol), nuqta bilan ajratilgan oddiy yo‘l stringi (5-misol) yoki funksiya (6-misol) bo‘lishi mumkin.
callback funksiyasi Majburiy. Ikkinchi argument — kuzatish manbaida o‘zgarish bo‘lganda ishga tushadigan callback funksiyasi. Callback funksiyasini kuzatish manbaining yangi va eski qiymatlarini argument sifatida qabul qiladigan qilib sozlash mumkin (1-misolga qarang).
opsiyalar obyekti Optional. Here we can specify the options deep, immediate, flush, and onTrigger/onTrack.

deep: standart qiymati "false". Agar kuzatuvchi deep bo‘lsa, u kuzatayotgan xususiyatning ichkarisidagi (quyi darajalardagi) o‘zgarishlarga ham javob beradi. (2-misolga qarang)

immediate: standart qiymati "false". Kuzatuvchini u yaratilishi bilanoq darhol ishga tushiradi. "immediate" "true" qilib o‘rnatilganda, kuzatuvchi birinchi marta ishga tushganda eski qiymat "undefined" bo‘ladi. (3-misolga qarang)

flush: standart qiymati "pre". Callback funksiyasi komponent render qilinishiga nisbatan qachon ishga tushishini belgilaydi. Mumkin bo‘lgan qiymatlar: "pre", "post" va "sync". Bu flush opsiyasidan ehtiyotkorlik bilan foydalaning.

onTrigger/onTrack: debug qilish uchun ishlatiladi. Faqat development (ishlab chiqish) rejimida ishlaydi.

Eslatma: Kuzatuvchilarni watch opsiyasi yordamida ham yaratish mumkin.


Ko‘proq misollar

1-misol

Har safar "value" data xususiyati o‘zgarganda eski va yangi qiymatlar bilan yangi xabar yozish uchun $watch() metodidan foydalanish.

<template>
  <h2>Example $watch() Method</h2>
  <p>Drag the slider to change the value so that the $watch() method is triggered. The callback function writes a message with the new and old values.</p>
  <div>
    <p><input type="range" min="0" max="10" v-model="value"> Current value: {{ value }}</p>
    <ol>
      <li v-for="x in results">{{ x }}</li>
    </ol>
  </div>
</template>

<script>
export default {
  data() {
    return {
      value: 4,
      results: []
    };
  },
  mounted() {
    this.$watch('value', function(newVal, oldVal) {
      this.results.push('Old value:'+oldVal+', new value: '+newVal)
    })
  }
};
</script>

<style scoped>
div {
  border: solid black 1px;
  padding: 10px;
}
</style>
Misolni ishga tushirish »

2-misol

deep watch opsiyasi "true" qilib o‘rnatilgan holda $watch() metodidan foydalanish. Endi kuzatuvchi "value" obyektining ichkarisidagi o‘zgarishlarni ham aniqlay oladi.

<template>
  <h2>Example $watch() Method</h2>
  <p>Register an extra hobby for Stuart. The hobbies are stored in an array inside the 'value' object. The $watch() method
    is triggered because the 'deep' option is set to 'true' so that the watcher also detects changes further inside the
    object.</p>
  <div>
    <p>Register an extra hobby for Stuart:</p>
    <p><input type="text" ref="inpText"></p>
    <button v-on:click="regHobby">Register</button>
    <ol>
      <li v-for="x in watchMessages">{{ x }}</li>
    </ol>
  </div>
  <p>Current 'value' object:</p>
  <pre>{{ this.value }}</pre>
</template>

<script>
export default {
  data() {
    return {
      value: {
        owner: 'Stuart',
        address: 'Faraway Lane',
        hobbies: ['Bird watching', 'Trail running']
      },
      watchMessages: []
    };
  },
  methods: {
    regHobby() {
      this.value.hobbies.push(this.$refs.inpText.value);
      this.$refs.inpText.value = null;
      this.$refs.inpText.focus();
    }
  },
  mounted() {
    this.$watch('value', function () {
      this.watchMessages.push('watcher triggered')
    }, {
      deep: true
    });
  }
};
</script>

<style scoped>
div {
  border: solid black 1px;
  padding: 10px;
}

li {
  background-color: lightgreen;
}</style>
Misolni ishga tushirish »

3-misol

immediate watch opsiyasi "true" qilib o‘rnatilgan holda $watch() metodidan foydalanish. Endi kuzatuvchi yaratilishi bilanoq darhol ishga tushadi.

<template>
  <h2>Example $watch() Method</h2>
  <p>With the 'immediate' option set to 'true' the watcher is also triggered right after it is created.</p>
  <div>
    <input type="range" min="0" max="10" v-model="value"> Current value: {{ value }}
    <p>Messages from the watcher:</p>
    <ol>
      <li v-for="x in watchMessages">{{ x }}</li>
    </ol>
  </div>
</template>

<script>
export default {
  data() {
    return {
      value: 4,
      watchMessages: []
    };
  },
  mounted() {
    this.$watch('value', (newVal, oldVal) => {
      this.watchMessages.push('Old value: '+oldVal+' New value: '+newVal)
    }, {
      immediate: true
    });
  }
};
</script>

<style scoped>
div {
  border: solid black 1px;
  padding: 10px;
}

li:first-child {
  background-color: lightgreen;
}</style>
Misolni ishga tushirish »

4-misol

Kuzatuvchini to‘xtatish uchun $watch() metodi qaytargan to‘xtatish (stop) funksiyasidan foydalanish.

<template>
  <h2>Example $watch() Method</h2>
  <p>Drag the slider to see the watcher work, click the stop button, and drag the slider again to confirm that the watcher has now stopped.</p>
  <div>
    <p><input type="range" min="0" max="10" v-model="value"> Current value: {{ value }}</p>
    <button v-on:click="stopFunc">Stop Watcher</button>
    <ol>
      <li v-for="x in results">{{ x }}</li>
    </ol>
  </div>
</template>

<script>
export default {
  data() {
    return {
      value: 4,
      results: [],
      stopFunc: null
    };
  },
  mounted() {
    this.stopFunc = this.$watch('value', function() {
      this.results.push('$watch() method')
    })
  }
};
</script>

<style scoped>
div {
  border: solid black 1px;
  padding: 10px;
}
</style>
Misolni ishga tushirish »

5-misol

$watch() metodi "value" obyekti ichidagi "country" xususiyatini kuzata olishi uchun nuqta bilan ajratilgan yo‘l stringidan foydalanish.

<template>
  <h2>Example $watch() Method</h2>
  <p>The watcher is set up to watch 'value.country' and will therefore detect when the country is changed inside the 'value' object.</p>
  <div>
    <p>Register a new country for Stuart to live in:</p>
    <p><input type="text" v-model="inpVal"></p>
    <button v-on:click="regHobby">Register</button>
    <ol>
      <li v-for="x in watchMessages">{{ x }}</li>
    </ol>
  </div>
  <p>Current 'value' object:</p>
  <pre>{{ this.value }}</pre>
</template>

<script>
export default {
  data() {
    return {
      inpVal: null,
      value: {
        owner: 'Stuart',
        address: 'Faraway Lane',
        country: 'Mexico'
      },
      watchMessages: []
    };
  },
  methods: {
    regHobby() {
      this.value.country = this.inpVal;
      this.inpVal = null;
    }
  },
  mounted() {
    this.$watch('value.country', function () {
      this.watchMessages.push('watcher triggered')
    });
  }
};
</script>

<style scoped>
div {
  border: solid black 1px;
  padding: 10px;
}
</style>
Misolni ishga tushirish »

6-misol

Bir nechta qiymatdagi o‘zgarishlarni kuzatish uchun $watch() metodida funksiyadan foydalanish.

<template>
  <h2>Example $watch() Method</h2>
  <p>Using a function as the first argument in the watcher to watch for changes in the sum of value A and value B.</p>
  <div>
    <p>Register a new country for Stuart to live in:</p>
    <p>Value A: <input type="range" min="-10" max="20" v-model="inpValA"> {{ inpValA }}</p>
    <p>Value B: <input type="range" min="-10" max="20" v-model="inpValB"> {{ inpValB }}</p>
    <ol>
      <li v-for="x in watchMessages">{{ x }}</li>
    </ol>
  </div>
</template>

<script>
export default {
  data() {
    return {
      inpValA: 2,
      inpValB: 4,
      watchMessages: []
    };
  },
  mounted() {
    this.$watch( 
      ()=> Number(this.inpValA) + Number(this.inpValB), 
      function (newVal,oldVal) {
        this.watchMessages.push('watcher triggered. A + B = ' + newVal)
      }
    );
  }
};
</script>

<style scoped>
div {
  border: solid black 1px;
  padding: 10px;
}
li {
  background-color: lightgreen;
}
</style>
Misolni ishga tushirish »


Tegishli sahifalar

Vue darsligi: Vue kuzatuvchilari (watchers)

Vue darsligi: Vue metodlari

Vue darsligi: Vue v-on direktivasi

Vue darsligi: Vue shablon reflari (template refs)

Vue ma’lumotnomasi: Vue $refs obyekti

JavaScript darsligi: JS Arrow funksiyasi


W3Schools Pathfinder

Yutuqlaringizni kuzating – bu bepul!