溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Vue組件內(nèi)部如何實(shí)現(xiàn)一個(gè)雙向數(shù)據(jù)綁定

發(fā)布時(shí)間:2020-10-16 15:12:44 來(lái)源:億速云 閱讀:168 作者:小新 欄目:web開(kāi)發(fā)

小編給大家分享一下Vue組件內(nèi)部如何實(shí)現(xiàn)一個(gè)雙向數(shù)據(jù)綁定,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

思路:父組件通過(guò)props傳值給子組件,子組件通過(guò) $emit 來(lái)通知父組件修改相應(yīng)的props值,具體實(shí)現(xiàn)如下:
import Vue from 'vue'
const component = {
    props: ['value'],
      template: `
        <div>
            <input type="text" @input="handleInput" :value="value">
        </div>
    `,
      data () {
          return{}
      },
      methods: {
        handleInput (e) {
            this.$emit('input', e.target.value)
        }
    }
}

new Vue({
    components: {
        CompOne: component
      },
      el: '#root',
      template: `
        <div>
          <comp-one :value1="value" @input="value = arguments[0]"></comp-one>
        </div>
      `,
  data () {
    return{
        value: '123'
    }
  }
})

看完了這篇文章,相信你對(duì)Vue組件內(nèi)部如何實(shí)現(xiàn)一個(gè)雙向數(shù)據(jù)綁定有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI