v-model
Misol
<input> elementi va data xususiyati o‘rtasida ikki tomonlama bog‘lanish (two-way binding) yaratish uchun v-model direktivasidan foydalanish.
<template>
<h1>v-model Example</h1>
<p>Write something, and see the 'inputValue' data property update automatically.</p>
<input type="text" v-model="inputValue">
<p>inputValue property: "{{ inputValue }}"</p>
</template>
Misolni ishga tushirish »
Quyida ko‘proq misollarni ko‘ring.
Ta’rif va qo‘llanilishi
v-model direktivasi forma kiritish elementi bilan, yoki Vue nusxasi xususiyati bilan komponent o‘rtasida ikki tomonlama bog‘lanish yaratish uchun ishlatiladi.
v-model bilan ishlatiladigan forma kiritish elementlari
v-model bilan ishlatilishi mumkin bo‘lgan forma kiritish elementlari: <input>, <select> va <textarea>.
Forma kiritish elementlarida v-model bilan ikki tomonlama bog‘lanish quyidagicha ishlaydi:
- Vue kiritish maydoni qiymatidagi o‘zgarishni aniqlaganda, tegishli data xususiyatini shunga mos ravishda yangilaydi. (HTML -> JavaScript)
- Vue nusxasi xususiyatidagi o‘zgarishni aniqlaganda, Vue tegishli kiritish maydoni qiymatini shunga mos ravishda yangilaydi. (JavaScript -> HTML)
(Yuqoridagi misolga va quyidagi 1-misolga qarang.)
v-model bilan ishlatiladigan komponentlar
Komponentda v-model ishlatilganda, ikki tomonlama bog‘lanishga erishish uchun komponent interfeysi props va emits yordamida to‘g‘ri sozlanishi kerak.
Komponentlarda v-model bilan ikki tomonlama bog‘lanish quyidagicha ishlaydi:
- Vue ota nusxa xususiyatidagi o‘zgarishni aniqlaganda, yangi qiymat komponentga prop sifatida yuboriladi.
- Vue bola komponentdagi o‘zgarishni aniqlaganda, yangi qiymat emit hodisasi sifatida ota komponentga yuboriladi.
Komponentda v-model ishlatilganda, standart prop nomi “modelValue”, standart emit hodisasi nomi esa “update:modelValue” bo‘ladi. (2-misol va 3-misolga qarang.)
Komponentda v-model ishlatilganda, Vue nusxasining data xususiyati o‘rniga get() va set() metodlariga ega computed xususiyatdan foydalanishimiz mumkin. (4-misolga qarang)
v-model: yordamida props va emits uchun standart "modelValue" va "update:modelValue" nomlaridan farqli nomlarni belgilash mumkin. (5-misolga qarang)
Komponentga ikki tomonlama bog‘lanish orqali bir nechta qiymatni ulash uchun har bir shunday qiymatni alohida v-model bilan belgilashimiz kerak. (6-misolga qarang)
Modifikatorlar
| Modifikatorlar | Tafsilotlar |
|---|---|
.lazy |
Sinxronlash vaqtini aniqlashda Vue input hodisasi o‘rniga change hodisasidan foydalanadi. Bu shuni anglatadiki, nusxa xususiyatining qiymati yangilanishi uchun foydalanuvchi avval kiritish maydonini o‘zgartirishi, so‘ngra fokusni kiritish elementidan boshqa joyga o‘tkazishi kerak. (7-misolga qarang) |
.number |
Kiritilgan qiymatni son (number) turiga aylantiradi. <input type="number"> ishlatilganda bu avtomatik ravishda bajariladi. |
.trim |
Kiritilgan qiymatning boshidagi va oxiridagi bo‘sh joylarni olib tashlaydi. (8-misolga qarang) |
| maxsus (custom) | v-model uchun maxsus modifikator yaratish uchun avval yangi modifikatorni saqlaydigan "modelModifiers" prop’ini aniqlashimiz kerak. Modifikator funksionalligi metod ichida yoziladi. Agar modifikator o‘rnatilgan bo‘lsa, qiymat ota komponentga emit qilib qaytarilishidan oldin metod ichida tegishli kod bajariladi. (9-misolga qarang) |
Ko‘proq misollar
1-misol
"inputValue" xususiyatining qiymatini o‘zgartirish uchun slayderdan (<input type="range">) foydalanish. <input type="text"> elementi avtomatik ravishda yangilanadi, chunki u v-model orqali "inputValue" xususiyatiga bog‘langan.
<template>
<h1>v-model Example</h1>
<p>Drag the slider to change the 'inputValue' data property, and see the input text field update automatically because of the two-way binding from v-model.</p>
<input type="range" min="-50" max="50" v-on:input="sliderChange" value="4">
<p>inputValue property: "{{ inputValue }}"</p>
<input type="text" v-model="inputValue">
</template>
<script>
export default {
data() {
return {
inputValue: null
};
},
methods: {
sliderChange(evt) {
this.inputValue = evt.target.value
}
}
}
</script>
Misolni ishga tushirish »
2-misol
Komponentda v-modelni props va emits bilan birga ishlatish, shunda <input> elementidagi o‘zgarishlar ota komponentning “text” xususiyatini yangilaydi.
App.vue:
<template>
<h2>Example v-model Directive</h2>
<p>App.vue 'text' property: "{{ text }}"</p>
<comp-one v-model="text"/>
</template>
<script>
export default {
data() {
return {
text: 'Say Cheese'
}
}
}
</script>
CompOne.vue:
<template>
<div>
<h3>Component</h3>
<p>Write something in the text input field below to see that changes here are emitted from the component, and the parent 'text' property gets updated by the use of v-model.</p>
<input
:value="modelValue"
@input="$emit('update:modelValue', $event.target.value)"
/>
</div>
</template>
<script>
export default {
props: ['modelValue'],
emits: ['update:modelValue']
}
</script>
<style scoped>
div {
border: solid black 1px;
padding: 10px;
margin: 20px 0;
max-width: 500px;
}
</style>
Misolni ishga tushirish »
3-misol
Ikki tomonlama bog‘lanishni yaqqolroq ko‘rsatish uchun komponentda v-model’dan foydalanish. Komponent ota komponentning "text" xususiyatini yangilay oladi, ota komponentning "text" xususiyati o‘zgarganda esa komponent yangilanadi.
App.vue:
<template>
<h2>Example v-model Directive</h2>
<p>App.vue 'text' property: "<pre>{{ text }}</pre>"</p>
<button v-on:click="this.text = 'Hello!'">text='Hello!'</button>
<comp-one v-model="text"/>
</template>
<script>
export default {
data() {
return {
text: 'Say Cheese'
}
}
}
</script>
<style>
pre {
display: inline;
background-color: yellow;
}
</style>
CompOne.vue:
<template>
<div>
<h3>Component</h3>
<p>Two-way binding on component with v-model:</p>
<ol>
<li>The component can update the 'text' property (using text field).</li>
<li>The component gets updated when the 'text' property is changed (using button).</li>
</ol>
<input
:value="modelValue"
@input="$emit('update:modelValue', $event.target.value)"
/>
</div>
</template>
<script>
export default {
props: ['modelValue'],
emits: ['update:modelValue']
}
</script>
<style scoped>
div {
border: solid black 1px;
padding: 10px;
margin: 20px 0;
max-width: 600px;
}
</style>
Misolni ishga tushirish »
4-misol
v-model’ni komponent ichida get() va set() funksiyalariga ega computed qiymat bilan ishlatish.
CompOne.vue:
<template>
<div>
<h3>Component</h3>
<p>Two-way binding on component with v-model:</p>
<ol>
<li>The component can update the 'text' property (using text field).</li>
<li>The component gets updated when the 'text' property is changed (using button).</li>
</ol>
<input v-model="inpVal"/>
</div>
</template>
<script>
export default {
props: ['modelValue'],
emits: ['update:modelValue'],
computed: {
inpVal: {
get() {
return this.modelValue;
},
set(inpVal) {
this.$emit('update:modelValue',inpVal)
}
}
}
}
</script>
<style scoped>
div {
border: solid black 1px;
padding: 10px;
margin: 20px 0;
max-width: 600px;
}
</style>
Misolni ishga tushirish »
5-misol
Standart prop nomi “modelValue”ni “message”ga o‘zgartirish uchun komponentda v-model:message’dan foydalanish.
App.vue:
<template>
<h2>Example v-model Directive</h2>
<p>App.vue 'text' property: "<pre>{{ text }}</pre>"</p>
<button v-on:click="this.text = 'Hello!'">text='Hello!'</button>
<comp-one v-model:message="text"/>
</template>
<script>
export default {
data() {
return {
text: 'Say Cheese'
}
}
}
</script>
<style>
pre {
display: inline;
background-color: yellow;
}
</style>
CompOne.vue:
<template>
<div>
<h3>Component</h3>
<p>Two-way binding on component with v-model:</p>
<ol>
<li>The component can update the 'text' property (using text field).</li>
<li>The component gets updated when the 'text' property is changed (using button).</li>
</ol>
<input
:value="message"
@input="$emit('update:message', $event.target.value)"
/>
</div>
</template>
<script>
export default {
props: ['message'],
emits: ['update:message']
}
</script>
<style scoped>
div {
border: solid black 1px;
padding: 10px;
margin: 20px 0;
max-width: 600px;
}
</style>
Misolni ishga tushirish »
6-misol
Ikki qiymat bilan ikki tomonlama bog‘lanish yaratish uchun komponentda v-model’dan ikki marta foydalanish.
App.vue:
<template>
<h2>Example v-model Directive</h2>
<p>Name: "<pre>{{ name }}</pre>"</p>
<p>Height: <pre>{{ height }}</pre> cm</p>
<comp-one
v-model:name="name"
v-model:height="height"
/>
</template>
<script>
export default {
data() {
return {
name: 'Olaf',
height: 120
}
}
}
</script>
<style>
pre {
display: inline;
background-color: yellow;
}
</style>
CompOne.vue:
<template>
<div>
<h3>Component</h3>
<p>Two inputs are bound to the component with v-model through props and emits.</p>
<p>
<label>
Name:
<input
type="text"
:value="name"
@input="$emit('update:name', $event.target.value)"
/>
</label>
</p>
<p>
<label>
Height:
<input
type="range"
:value="height"
@input="$emit('update:height', $event.target.value)"
min="50"
max="200"
/>
{{ this.$props.height }} cm
</label>
</p>
</div>
</template>
<script>
export default {
props: ['name','height'],
emits: ['update:name','update:height']
}
</script>
<style scoped>
div {
border: solid black 1px;
padding: 10px;
margin: 20px 0;
max-width: 300px;
}
</style>
Misolni ishga tushirish »
7-misol
.lazy’dan foydalanish: bunda xususiyat v-model orqali yangilanishi uchun foydalanuvchi avval kiritish elementini o‘zgartirishi, so‘ngra fokusni kiritish elementidan boshqa joyga o‘tkazishi kerak.
<template>
<h1>v-model Example</h1>
<p>Using the '.lazy' modifier, you must first write something, then click somewhere else, or use the tab key to switch focus away from the input element, before the property get updated.</p>
<input type="text" v-model.lazy="inputValue">
<p>inputValue property: "{{ inputValue }}"</p>
</template>
<script>
export default {
data() {
return {
inputValue: null
};
}
}
</script>
Misolni ishga tushirish »
8-misol
.lazy’dan foydalanish: bunda xususiyat v-model orqali yangilanishi uchun foydalanuvchi avval kiritish elementini o‘zgartirishi, so‘ngra fokusni kiritish elementidan boshqa joyga o‘tkazishi kerak.
<template>
<h1>v-model Example</h1>
<p>Using the '.trim' modifier will remove any white spaces at the start and end of the input.</p>
<p>Add white spaces at the start and end in the input fields below to see the difference with or with out '.trim'.</p>
<p>No '.trim': <input type="text" v-model="inputVal1"> "<pre>{{ inputVal1 }}</pre>"</p>
<p>With '.trim': <input type="text" v-model.trim="inputVal2"> "<pre>{{ inputVal2 }}</pre>"</p>
</template>
<script>
export default {
data() {
return {
inputVal1: 'Hello',
inputVal2: 'Hi'
};
}
}
</script>
<style>
pre {
display: inline;
background-color: lightgreen;
}
</style>
Misolni ishga tushirish »
9-misol
Agar .allCapital modifikatori o‘rnatilgan bo‘lsa, kiritilgan barcha belgilarni katta harfga aylantirish uchun maxsus .allCapital modifikatoridan foydalanish.
App.vue:
<template>
<h2>Example v-model Directive</h2>
<p>App.vue 'text' property: "{{ text }}"</p>
<comp-one v-model.allCapital="text"/>
</template>
<script>
export default {
data() {
return {
text: ''
}
}
}
</script>
CompOne.vue:
<template>
<div>
<h3>Component</h3>
<p>Write something in the text input field below. Click somewhere else or use the tab key to shift focus away from the input element to see the effect of the custom 'allCapital' modifier.</p>
<input
:value="modelValue"
@change="this.emitVal"
/>
</div>
</template>
<script>
export default {
props: {
modelValue: String,
modelModifiers: {
// modelModifiers is an empty object initially.
// Modifiers set on the component will be stored here.
default: () => ({})
}
},
emits: ['update:modelValue'],
methods: {
emitVal(e) {
let value = e.target.value
if (this.modelModifiers.allCapital) {
value = value.toUpperCase()
}
this.$emit('update:modelValue', value)
}
}
}
</script>
<style scoped>
div {
border: solid black 1px;
padding: 10px;
margin: 20px 0;
max-width: 500px;
}
</style>
Misolni ishga tushirish »
Tegishli sahifalar
Vue darsligi: Vue komponentlari
Vue darsligi: Vue props
Vue darsligi: Vue $emit() metodi
Vue darsligi: Vue hisoblangan (computed) xususiyatlar
Vue ma’lumotnomasi: Vue $emit() metodi
Vue ma’lumotnomasi: Vue $props obyekti
JavaScript qo‘llanmasi: JavaScript obyektlarida getter va setter
W3Schools Pathfinder
Yutuqlaringizni kuzating – bu bepul!
