vue如何在自定義組件中使用v-model
v-model指令
所謂的“指令”其實(shí)就是擴(kuò)展了HTML標(biāo)簽功能(屬性)。
先來一個組件,不用vue-model,正常父子通信
<!-- parent --> <template> <div class="parent"> <p>我是父親, 對兒子說: {{sthGiveChild}}</p> <Child @returnBack="turnBack" :give="sthGiveChild"></Child> </div> </template> <script> import Child from './Child.vue'; export default { data() { return { sthGiveChild: '給你100塊' }; }, components: { Child }, methods: { turnBack(val) { this.sthGiveChild = val; } } } </script>
<!-- child --> <template> <div class="child"> <p>我是兒子,父親對我說: {{give}}</p> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="returnBackFn">回應(yīng)</a> </div> </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> <div class="parent"> <p>我是父親, 對兒子說: {{sthGiveChild}}</p> <Child v-model="sthGiveChild"></Child> </div> </template> <script> import Child from './Child.vue'; export default { data() { return { sthGiveChild: '給你100塊' }; }, components: { Child } } </script>
<!-- child --> <template> <div class="child"> <p>我是兒子,父親對我說: {{give}}</p> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="returnBackFn">回應(yīng)</a> </div> </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)情況下,一個組件上的 v-model 會把 value 用作 prop 且把 input 用作 event。
嘗試把上邊子組件的例子改一下,也是跑的通的
<!-- child --> <template> <div class="child"> <p>我是兒子,父親對我說: {{value}}</p> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="returnBackFn">回應(yīng)</a> </div> </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。我在寫這個速記的時候,多寫了一個s,調(diào)試到懷疑人生 event: 'someEvent' } this.$emit('someProp', [returnValueToParent])
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue.js項目中管理每個頁面的頭部標(biāo)簽的兩種方法
這篇文章主要介紹了Vue.js項目中管理每個頁面的頭部標(biāo)簽的兩種方法,需要的朋友可以參考下2018-06-06Vue3中使用Element?Plus時el-icon無法顯示的問題解決
我們的Vue前端一般都是用的ElementUI,其中按鈕可能用到的比較多,官方里面有自帶的一些默認(rèn)圖標(biāo),下面這篇文章主要給大家介紹了關(guān)于Vue3中使用Element?Plus時el-icon無法顯示的問題解決,需要的朋友可以參考下2022-03-03Vue3中級指南之如何在vite中使用svg圖標(biāo)詳解
在以webpack為構(gòu)建工具的開發(fā)環(huán)境中我們可以很方便的實(shí)現(xiàn)SVG圖標(biāo)的組件化,下面這篇文章主要給大家介紹了關(guān)于Vue3中級指南之如何在vite中使用svg圖標(biāo)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04Vue請求JSON Server服務(wù)器數(shù)據(jù)的實(shí)現(xiàn)方法
這篇文章主要介紹了Vue請求JSON Server服務(wù)器數(shù)據(jù)的實(shí)現(xiàn)方法,需要的朋友可以參考下2018-11-11vuejs+element UI table表格中實(shí)現(xiàn)禁用部分復(fù)選框的方法
今天小編就為大家分享一篇vuejs+element UI table表格中實(shí)現(xiàn)禁用部分復(fù)選框的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09vue菜單欄聯(lián)動內(nèi)容頁面tab的實(shí)現(xiàn)示例
本文主要介紹了vue菜單欄聯(lián)動內(nèi)容頁面tab的實(shí)現(xiàn)示例,左側(cè)菜單欄與右側(cè)內(nèi)容部分聯(lián)動,當(dāng)點(diǎn)擊左側(cè)的菜單,右側(cè)會展示對應(yīng)的tab,具有一定的參考價值,感興趣的可以了解一下2024-01-01