Vue2子組件綁定 v-model,實(shí)現(xiàn)父子組件通信方式
前言
v-model 可以在組件上使用以實(shí)現(xiàn)雙向綁定。
首先讓我們回憶一下 v-model 在原生元素上的用法:
<input v-model="firstName" />
在代碼背后,模板編譯器會(huì)對(duì) v-model 進(jìn)行更冗長(zhǎng)的等價(jià)展開(kāi)。因此上面的代碼其實(shí)等價(jià)于下面這段:
<input :value="firstName" @input="firstName = $event.target.value" />
默認(rèn)用法
父組件
<template> <div> <h1>{{ first }}-{{ last }}</h1> <UserName :firstName="first" :lastName="last" @update:firstName="func1" @update:lastName="func2" /> </div> </template> <script> import UserName from "./UserName.vue"; export default { name: "V-Model", components: { UserName, }, data() { return { first: "000", last: "123", }; }, methods: { // 默認(rèn)用法 func1(val) { this.first = val; }, func2(val) { this.last = val; }, }, }; </script>
子組件
<template> <div> <input v-model="first" type="text" @input="input1" /> <input v-model="last" type="text" @input="input2" /> </div> </template> <script> export default { name: "UserName", props: ["firstName", "lastName"], data() { return { first: this.firstName, last: this.lastName, }; }, methods: { input1() { this.$emit("update:firstName", this.first); }, input2() { this.$emit("update:lastName", this.last); }, }, }; </script>
以上就可以實(shí)現(xiàn) 父子組件的雙向綁定
.sync寫(xiě)法
傳參的時(shí)候加上 .sync 會(huì)簡(jiǎn)化上面的寫(xiě)法,父組件不需要定義更新觸發(fā)函數(shù)(update)
<UserName :firstName.sync="first" />
會(huì)被擴(kuò)展為:
<UserName :firstName="first" @update:firstName="val => first = val"></UserName>
當(dāng)子組件需要更新 firstName 的值時(shí),它需要顯式地觸發(fā)一個(gè)更新事件:
this.$emit('update:firstName', newValue)
父組件
<template> <div> <h1>{{ first }}-{{ last }}</h1> <UserName :firstName.sync="first" :lastName.sync="last" /> </div> </template> <script> import UserName from "./UserName.vue"; export default { name: "V-Model", components: { UserName, }, data() { return { first: "000", last: "123", }; }, methods: { }, }; </script>
子組件
<template> <div> <input type="text" :value="firstName" @input="$emit('update:firstName', $event.target.value)" /> <input type="text" :value="lastName" @input="$emit('update:lastName', $event.target.value)" /> </div> </template> <script> export default { name: "UserName", components: {}, props: ["firstName", "lastName"], data() { return {}; }, methods: {}, }; </script>
以上也可以實(shí)現(xiàn) 父子組件的雙向綁定
綁定單個(gè) v-model
當(dāng)使用在一個(gè)組件上時(shí),v-model 會(huì)被展開(kāi)為如下的形式:
<UserName v-model="first" /> <!-- 上面等同于下面的寫(xiě)法 --> <UserName :modelValue="first" @input="first= $event.target.value" />
父組件
<template> <div> <h1>{{ first }}-{{ last }}</h1> <UserName v-model="first" /> </div> </template> <script> import UserName from "./UserName.vue"; export default { name: "V-Model", components: { UserName, }, data() { return { first: "000", last: "123", }; }, methods: { }, }; </script>
子組件
<template> <div> <input type="text" :value="firstName" @input="$emit('update', $event.target.value)" /> </div> </template> <script> export default { name: "UserName", components: {}, props: ["firstName"], model: { prop: "firstName", event: "update", }, data() { return {}; }, }; </script>
現(xiàn)在可以實(shí)現(xiàn)單個(gè) 輸入框綁定
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 在Vue2中v-model和.sync區(qū)別解析
- vue2和vue3組件v-model區(qū)別詳析
- vue2中如何自定義組件的v-model
- Vue v-model相關(guān)知識(shí)總結(jié)
- vue2 v-model/v-text 中使用過(guò)濾器的方法示例
- vue 2.0組件與v-model詳解
- Vue2.0利用 v-model 實(shí)現(xiàn)組件props雙向綁定的優(yōu)美解決方案
- vue2 如何實(shí)現(xiàn)div contenteditable=“true”(類(lèi)似于v-model)的效果
- vue2與vue3雙向數(shù)據(jù)綁定的區(qū)別說(shuō)明
- vue 2 實(shí)現(xiàn)自定義組件一到多個(gè)v-model雙向數(shù)據(jù)綁定的方法(最新推薦)
相關(guān)文章
基于Vue3實(shí)現(xiàn)一個(gè)簡(jiǎn)單的方位動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了如何基于Vue3實(shí)現(xiàn)一個(gè)簡(jiǎn)單的方位動(dòng)畫(huà),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02Vue3.0插件執(zhí)行原理與實(shí)戰(zhàn)
這篇文章主要介紹了Vue3.0插件執(zhí)行原理與實(shí)戰(zhàn),Vue項(xiàng)目能夠使用很多插件來(lái)豐富自己的功能Vue-Router、Vuex等,節(jié)省了我們大量的人力和物力,下面我們就一起來(lái)了解Vue3.0插件的原理吧,需要的小伙伴可以參考一下2022-02-02100行代碼理解和分析vue2.0響應(yīng)式架構(gòu)
通過(guò)100行代碼幫助大家理解和分析vue2.0響應(yīng)式架構(gòu)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03vue項(xiàng)目?jī)?yōu)雅實(shí)現(xiàn)自動(dòng)引入組件的方法詳解
在我們寫(xiě)vue項(xiàng)目的時(shí)候,都會(huì)引入一些組件庫(kù),有時(shí)候有可能還不止一個(gè)組件庫(kù),那么如何優(yōu)雅的實(shí)現(xiàn)自動(dòng)引入組件呢,下面小編就來(lái)和大家詳細(xì)講講吧2023-09-09vue自定義鍵盤(pán)信息、監(jiān)聽(tīng)數(shù)據(jù)變化的方法示例【基于vm.$watch】
這篇文章主要介紹了vue自定義鍵盤(pán)信息、監(jiān)聽(tīng)數(shù)據(jù)變化的方法,結(jié)合實(shí)例形式分析了vue.js基于vm.$watch進(jìn)行事件監(jiān)聽(tīng)相關(guān)操作技巧,需要的朋友可以參考下2019-03-03vue3使用Electron打包成exe的方法與打包報(bào)錯(cuò)解決
在前端開(kāi)發(fā)中,Electron是一種常用的工具,它允許開(kāi)發(fā)者使用Web技術(shù)構(gòu)建桌面應(yīng)用程序,本文主要介紹了vue3使用Electron打包成exe的方法與打包報(bào)錯(cuò)解決,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06