手动实现v-model功能(数据双向绑定)

文章通过一个简单的HTML示例展示了如何手动实现Vue的数据双向绑定功能,主要涉及Vue构造函数、Observer、Watcher以及Compile方法。在输入框(input)中应用v-model指令,当输入值改变时,更新视图;同时,视图变化时也会同步更新模型数据。
摘要由CSDN通过智能技术生成

1.Vue的实现原理,相信大家都不陌生了,主要三大函数,Observe, Watcher, Compile,今天记录下手动实现数据双向绑定的功能,直接上代码:

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">​
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta data-vue-meta="true" http-equiv="X-UA-Compatible" content="IE=edge">
</head>

<body>
    <div id="app">
        <input type="text" v-model="msg">
        <h3>{{msg}}</h3>
    </div>
    <script>
        function Vue(options = {}) {
            this.$options = options
            this.$el = document.querySelector(options.el)
            this._data = options.data
            this._watcher = {}
            this._observer(this._data)
            this._compile(this.$el)
        }
        Vue.prototype._observer = function (obj) {
            Object.keys(obj).forEach((key) => {
                this._watcher[key] = {
                    _directives: []
                }
                let watcher = this._watcher[key]
                let value = obj[key]
                Object.defineProperty(this._data, key, {
                    configurable: true,
                    enumerable: true,
                    get() {
                        console.log(`${key}获取的值是${value}`)
                        return value
                    },
                    set(newVlue) {
                        if (newVlue !== value) {
                            value = newVlue
                            console.log(`将订阅池里进行对应属性进行处罚更新`)
                            watcher._directives.forEach(item => {
                                item.update()
                            })
                        }
                    }
                })
            })
        }
        function Watcher(el, vm, val, attr) {
            this.el = el
            this.vm = vm
            this.val = val
            this.attr = attr
            this.update()
        }
        Watcher.prototype.update = function () {
            this.el[this.attr] = this.vm._data[this.val]
        }
        Vue.prototype._compile = function (el) {
            console.log(el.children)
            let nodes = el.children;
            const len = nodes.length;
            for (let i = 0; i < len; ++i) {
                let node = nodes[i];
                if (node.children.length) {
                    // 递归遍历
                    this._compile(node)
                }
                // 判断是否有v-mode指令
                if (node.hasAttributes('v-model') && node.tagName === 'INPUT') {
                    node.addEventListener('input', ((key) => {
                        let attVal = node.getAttribute('v-model')
                        console.log('attVal', attVal)
                        // 创建watcher 对象,并且将订阅器根据属性放入对应的订阅器集合
                        let watcher = new Watcher(node, this, attVal, 'value')
                        console.log(watcher)
                        this._watcher[attVal]._directives.push(watcher)
                        return () => {
                            this._data[attVal] = nodes[key].value
                        }
                    })(i))
                }
                // 匹配模板
                let reg = /\{\{\s*(.*?)\s*\}\}/igs
                let txt = node.textContent
                if (reg.test(txt)) {
                    node.textContent = txt.replace(reg, (match, placeholder) => {
                        //  console.log(match,plachoder)
                        let attrVal = placeholder
                        let watcher = new Watcher(node, this, attrVal, 'innerHTML')
                        this._watcher[attrVal]._directives.push(watcher)
                        return placeholder.split('.').reduce((val, key) => {
                            return val[key]
                        }, this._data)
                    })
                }
            }


        }
    </script>
    <script>
        const vue = new Vue({
            el: '#app',
            data: {
                msg: 'hello world'
            }
        })
    </script>
</body>

</html>

效果如下:在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值