手搓V-model双向绑定实现原理-基于defineProperty

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <input type="text" v-model="name" placeholder="name">
        <input type="text" v-model="age" placeholder="age">
        <input type="text" v-model="email" placeholder="email">
        <input type="text" v-model="tel" placeholder="tel">

        <br>

        <p>姓名:<span>{{ name }}</span></p>
        <p>年龄:<span>{{ age }}</span></p>
        <p>邮箱:<span>{{ email }}</span></p>
        <p>电话:<span>{{ tel }}</span></p>
    </div>
    <script>
        class MVVM {
            constructor(el, data) {
                this.el = document.querySelector(el)
                this._data = data
                this.domPool = {}
                this.init()
            }
            init() {
                this.initData()
                this.initDom()
            }
            initDom() {
                this.bindDom(this.el)
                this.bindInput(this.el)
            }
            initData() {
                this.data = {}
                const _this = this
                for (const key in this._data) {
                    Object.defineProperty(this.data, key, {
                        get() {
                            return _this._data[key]
                        },
                        set(newVal) {
                            _this.domPool[key].innerHTML = newVal
                            _this._data[key] = newVal
                        }
                    })
                }
            }
            bindInput(el) {
                const nodeList = document.querySelectorAll('input')
                nodeList.forEach(item => {
                    const _VModel = item.getAttribute('v-model')
                    if (!_VModel) return
                    item.addEventListener('keyup', this.handleInput.bind(this, _VModel, item), false)
                })
            }
            handleInput(key, el) {
                const value = el.value
                this.data[key] = value
            }
            bindDom(el) {
                const childs = el.childNodes
                childs.forEach(item => {
                    if (item.nodeType === 3) {
                        const _value = item.nodeValue
                        if (_value.trim().length) {
                            let isValid = /\{\{(.+?)\}\}/.test(_value)
                            if (isValid) {
                                const _key = _value.match(/\{\{(.+?)\}\}/)[1].trim()
                                this.domPool[_key] = item.parentNode
                                item.parentNode.innerText = this._data[_key] || undefined
                            }
                        }
                    }
                    item.childNodes && this.bindDom(item)
                })
            }

        }
    </script>
    <script>

        new MVVM('#app', {
            name: '1',
            age: '1',
            email: '1',
            tel: '1'
        })

    </script>
</body>
</html>```

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值