簡單實現一個VUE的雙向繫結
阿新 • • 發佈:2019-09-08
首先我們來看一些,vue的基本使用方法
new Vue({ el:'#app', data:{ price:27, info:{ title:'豬肉的價格' }, name:23 }, beforeCreate() { // console.log(this.info) }, created(){ // console.log(this.info) }, beforeMount () { // console.log(document.querySelector('#home')) }, mounted(){ // console.log(document.querySelector('#home')) }, render(createElement) { return createElement('div',{ attrs:{ title:this.info.title, id:'home' } },[ createElement('span',{},this.price) ]) } })
然後我們根據使用方法,來設計一個類和一些基礎的宣告週期
class Vue {
constructor(options) {
this.$el = document.querySelector(options.el) // 獲取根元素
this.beforeCreate = options.beforeCreate // 生命週期 beforeCreate
if(this.beforeCreate)this.beforeCreate()
this._data = options.data || options.data() // 獲取初始資料
this._render = options.render // 獲取渲染函式
new Observer(this._data) // 進行深度代理
for ( let key in this._data){ // 代理data 元素,把元素代理到this例項上
proxy(this,this._data,key)
}
this.created = options.created // 生命週期 created
if(this.created)this.created()
this.beforeMount = options.beforeMount // 生命週期 beforeMount
if(this.beforeMount)this.beforeMount()
new Watch(()=>{
this._update() // 渲染頁面
})
this.mounted = options.mounted // 生命週期 mounted
if(this.mounted)this.mounted()
}
_update () { // 渲染函式
const node = this._render(this._createElement) // 執行渲染函式
replaceChild(node,this.$el)
this.$el = node
}
_createElement (targetName,data,chilren) {
const tag = document.createElement(targetName)
const {attrs = {}} = data
for ( let attr in attrs){
tag.setAttribute(attr,attrs[attr])
}
if(Object.prototype.toString.call(chilren) !== '[object Array]'){
let child = document.createTextNode(chilren) // 建立一個文字節點
tag.appendChild(child)
}else {
chilren.forEach(child => {
tag.appendChild(child)
})
}
return tag
}
}
function replaceChild (newNode,oldNode) {
return oldNode.parentElement.replaceChild(newNode,oldNode)
}
function proxy (target,data,key) { // 代理函式,代理最外層data
Object.defineProperty(target,key,{
get () {
return data[key]
},
set (newValue) {
data[key] = newValue
}
})
}
function defineProperty (target,key,value) { // 深度監聽
const dep = new Dep()
Object.defineProperty(target,key,{
get () {
if(Dep.targets.length>0){ // 為了排除後面來的
dep.addDepend()
}
return value
},
set (newValue) {
value = newValue
dep.notify()
}
})
}
class Dep { // 釋出訂閱者
constructor(arg) {
this.subs = []
}
addSub(obj){ // 新增,
if(this.subs.indexOf(obj) === -1) { // 如不存在該物件,則新增該物件
this.subs.push(obj)
}
}
notify(){ // 釋出
for(let i = 0;i<this.subs.length;i++){
this.subs[i].update()
}
}
addDepend () {
Dep.targets[Dep.targets.length-1].addDep(this)
}
}
Dep.targets = [] // 新增
function pushTarget (instance) { // 入棧
Dep.targets.push(instance)
}
function popTarget () { // 出棧
return Dep.targets.pop()
}
class Watch { // 事件監聽類
constructor(getter){
this.getter = getter
this.get()
}
get () {
pushTarget(this)
let value = this.getter()
popTarget()
return value
}
addDep (dep) {
dep.addSub(this)
}
update () {
this.getter()
}
}
class Observer {
constructor(obj) {
this.walk(obj)
}
walk (obj) {
for(let key in obj){
if(typeof obj[key] === 'object'){
this.walk(obj[key])
}
defineProperty (obj,key,obj[key