Vue使用.sync 實(shí)現(xiàn)父子組件的雙向綁定數(shù)據(jù)

1.前言

最近在vue 項(xiàng)目中有一個(gè)需求, 就是我需要根據(jù)不同的類(lèi)型在頁(yè)面中放不同的組件, 組件需要跟當(dāng)前頁(yè)面的數(shù)據(jù)進(jìn)行雙向綁定,如果都寫(xiě)在同一個(gè)頁(yè)面 代碼會(huì)顯得比較多,畢竟我當(dāng)前頁(yè)面已經(jīng)7-800行代碼了 所以我需要把一些元素定義成組件 ,封裝起來(lái),所以就會(huì)遇到 數(shù)據(jù)的傳值綁定問(wèn)題
在這里我就分享我的方法,也許很多博客上有過(guò)!

2.父組件

首先我們來(lái)看看官方文檔 [https://cn.vuejs.org/v2/guide/components.html#sync-修飾符]

.sync 修飾符所提供的功能。當(dāng)一個(gè)子組件改變了一個(gè) prop 的值時(shí),這個(gè)變化也會(huì)同步到父組件中所綁定
就是說(shuō)我們可以直接在我們需要傳的prop后面加上 .sync
比如 我下面需要綁定 p_model,然后我在他后面加上.sync

<certificate-input
              :p_model.sync='pname'>
   </certificate-input>

他會(huì)擴(kuò)展成:

 <certificate-input  :p_model="pname" @update:p_model="val => pname= val"></certificate-input>

父組件全部代碼:

<template>
 <div>
   <certificate-input
              :p_model.sync='pname'
              :xi_model.sync="xiname">
   </certificate-input>
</div>
</template>
import CertificateInput from '../common/CertificateInput.vue'
export default {
    name: 'fathor',
    components: {
        CertificateInput 
    },
    data() {
      return {
            pname:"",
            xiname:""
    }     
}

子組件

上面說(shuō)了一大推父組件下面我們來(lái)看看子組件怎么寫(xiě) ?
因?yàn)槲翼?xiàng)目中使用vux 代碼就直接復(fù)制過(guò)來(lái)改了一下

<template>
    <div>
      <x-input
            title="姓名"  
            v-model="name" 
            ></x-input>
        <x-input title="身份證號(hào)" 
                v-model="idCard" 
                placeholder="請(qǐng)輸入身份證號(hào)"
                required>
        </x-input>
    </div>
</template>
<script type="text/javascript">
    import { XInput} from 'vux'
    export default{
        name:'certificateInput',
        props:["p_model","xi_model"],
        components:{
            XInput
        },
        data(){
            return{
                name:this.p_model,
                idCard:this.xi_model
            }
        },
        watch:{
      
            p_model(val) { 
                this.address = val;
            },
            name(val){
      //設(shè)置監(jiān)聽(tīng),如果改變就更新 p_model
                this.$emit('update:p_model', val)
            },
            xi_model(val){
                this.certificate = val
            },
            idCard(val){
                 this.$emit('update:xi_model', val)
            }
        }
    }
</script>

由上面可以看出 子組件主要代碼 就是監(jiān)聽(tīng)他的改變 然后觸發(fā)父組件監(jiān)聽(tīng)的事件

   name(val){
      //設(shè)置監(jiān)聽(tīng),如果改變就更新 p_model
             this.$emit('update:p_model', val)
}

好了 上面就是我的方法
感覺(jué)寫(xiě)的好low
以后多多改善

后面的修改:

在上文中我試用了watch 去監(jiān)聽(tīng) 其實(shí)這可以換種方式來(lái)操作
// 如:watch中的

p_model(val) { 
          this.address = val;
            },

可以改成

computed: {
  _model:{
   get() {
    return this.p_model
   },
  set(val) {
    this.$emit("update:p_model",val)
  }
  }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容