如何在vue中使用v-model指令進(jìn)行數(shù)據(jù)綁定

如何在vue中使用v-model指令進(jìn)行數(shù)據(jù)綁定?針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

創(chuàng)新互聯(lián)建站長期為成百上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為邊壩企業(yè)提供專業(yè)的成都網(wǎng)站制作、成都做網(wǎng)站,邊壩網(wǎng)站改版等技術(shù)服務(wù)。擁有十年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

先來一個(gè)組件,不用vue-model,正常父子通信

<!-- parent -->

<template>

<p class="parent">

 <p>我是父親, 對兒子說: {{sthGiveChild}}</p>

 <Child @returnBack="turnBack" :give="sthGiveChild"></Child>

</p>

</template>

<script>

import Child from './Child.vue';

export default {

 data() {

  return {

   sthGiveChild: '給你100塊'

  };

 },

 components: {

  Child

 },

 methods: {

  turnBack(val) {

   this.sthGiveChild = val;

  }

 }

}

</script>
<!-- child -->

<template>

<p class="child">

 <p>我是兒子,父親對我說: {{give}}</p>

 <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow"   @click="returnBackFn">回應(yīng)</a>

</p>

</template>

<script>

export default {

 props: {

  give: String

 },

 methods: {

  returnBackFn() {

   this.$emit('returnBack', '還你200塊');

  }

 }

}
</script>

點(diǎn)擊回應(yīng)后,父親對兒子說的話變成了兒子的回應(yīng)。兒子收到的信息也變了,實(shí)現(xiàn)通信。

改用v-model

<!-- parent -->

<template>

<p class="parent">

 <p>我是父親, 對兒子說: {{sthGiveChild}}</p>

 <Child v-model="sthGiveChild"></Child>

</p>

</template>

<script>

import Child from './Child.vue';

export default {

 data() {

  return {

   sthGiveChild: '給你100塊'

  };

 },

 components: {

  Child

 }

}
</script>
<!-- child -->

<template>

<p class="child">

 <p>我是兒子,父親對我說: {{give}}</p>

 <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow"   @click="returnBackFn">回應(yīng)</a>

</p>

</template>

<script>

export default {

 props: {

  give: String

 },

 model: {

  prop: 'give',

  event: 'returnBack'

 },

 methods: {

  returnBackFn() {

   this.$emit('returnBack', '還你200塊');

  }

 }

}
</script>

文案雖有不同,但是效果最終是一致的。

看看官方自定義組件的v-model

官方例子https://vuefe.cn/v2/api/#model

有這么一句話: 默認(rèn)情況下,一個(gè)組件上的 v-model 會(huì)把 value 用作 prop 且把 input 用作 event。

嘗試把上邊子組件的例子改一下,也是跑的通的

<!-- child -->

<template>

<p class="child">

 <p>我是兒子,父親對我說: {{value}}</p>

 <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow"   @click="returnBackFn">回應(yīng)</a>

</p>

</template>

<script>

export default {

 props: {

  value: String

 },

 methods: {

  returnBackFn() {

   this.$emit('input', '還你200塊');

  }

 }

}
</script>

做一下總結(jié):

如果你懶,不想自己去處理事件,那就用默認(rèn)的 'value' && 'input' 事件去處理,如果用原生事件的,甚至連model屬性也可以省去。

如果你想自己的代碼比較明確,區(qū)分出自定義事件,那么下面的組合才是你的菜。

prop和event看你自己心情定義,當(dāng)然要知名見意【盡量避開關(guān)鍵字】

model: {
prop: 'someProp', // 注意,是prop,不帶s。我在寫這個(gè)速記的時(shí)候,多寫了一個(gè)s,調(diào)試到懷疑人生
event: 'someEvent'
}
this.$emit('someProp', [returnValueToParent])

vue是什么

Vue是一套用于構(gòu)建用戶界面的漸進(jìn)式JavaScript框架,Vue與其它大型框架的區(qū)別是,使用Vue可以自底向上逐層應(yīng)用,其核心庫只關(guān)注視圖層,方便與第三方庫和項(xiàng)目整合,且使用Vue可以采用單文件組件和Vue生態(tài)系統(tǒng)支持的庫開發(fā)復(fù)雜的單頁應(yīng)用。

關(guān)于如何在vue中使用v-model指令進(jìn)行數(shù)據(jù)綁定問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

文章題目:如何在vue中使用v-model指令進(jìn)行數(shù)據(jù)綁定
網(wǎng)頁鏈接:http://bm7419.com/article34/geejse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司靜態(tài)網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)關(guān)鍵詞優(yōu)化、電子商務(wù)、軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)