var MyVue = function (options) {
var keys = Object.keys(options.data)
this._data = {}
keys.forEach((key) => {
Object.defineProperty(this, key, {
get () {
return this['_data'][key]
},
set (val) {
this['_data'][key] = val
options.updated && options.updated.call(this, key, val)
}
})
this[key] = options.data[key]
})
// methods
for (fn in options.methods) {
this[fn] = options.methods[fn]
}
options.mounted && options.mounted.call(this)
}
var mv = new MyVue({
data: {
num: 1
},
updated (key, val) {
var el = document.getElementById('root')
el.innerHTML = val
},
methods: {
add () {
this.num += 100
}
},
mounted () {
setInterval(() => {
console.log('this', this)
this.add()
// this.num++
}, 2000);
}
})
简单实现vue
最新推荐文章于 2022-03-24 10:38:11 发布