-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from liuwu1211/develop
增加部分注释
- Loading branch information
Showing
6 changed files
with
110 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,20 @@ | ||
/** | ||
* 为了收集所有的watcher | ||
* 依赖跟踪类 | ||
* dep是可以有多个指令订阅的可观察对象 | ||
*/ | ||
class Dep { | ||
constructor() { | ||
this.subs = []; | ||
// 订阅任务队列,方式有相同的任务,用Set数据结构简单处理 | ||
this.subscribers = []; | ||
} | ||
// 收集观察者 | ||
// 收集观察者,用于收集依赖项,在vue源码中使用depend来收集依赖项,由于这是简化的,所以我们直接用addSub | ||
addSub(watcher) { | ||
this.subs.push(watcher); | ||
this.subscribers.push(watcher); | ||
} | ||
// 通知观察者去更新 | ||
// 用于发布消息,触发依赖项重新执行,通知观察者去更新 | ||
notify() { | ||
console.log('通知了观察者', this.subs); | ||
this.subs.forEach(w => w.update()); | ||
console.log('通知了观察者', this.subscribers); | ||
this.subscribers.forEach(w => w.update()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,4 +29,4 @@ class Watcher { | |
Dep.target = null; | ||
return oldVle; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Title</title> | ||
</head> | ||
<body> | ||
<div id="app"> | ||
<h2>{{person.name}} -- {{person.age}}</h2> | ||
<h3>{{person.fav}}</h3> | ||
<ul> | ||
<li>1</li> | ||
<li>2</li> | ||
<li>3</li> | ||
<li>{{test}}</li> | ||
</ul> | ||
<h3>{{msg}}</h3> | ||
<div v-text="msg"></div> | ||
<div v-text="person.name"></div> | ||
<div v-html="htmlStr"></div> | ||
<input type="text" v-model="msg"> | ||
<button v-on:click="handlerClick">双击on-666</button> | ||
<button @click="handlerClick">双击@-888</button> | ||
<img v-bind:src="imgSrc"/> | ||
</div> | ||
<script src="./js/source/Vue.js"></script> | ||
<script> | ||
let vm = new Vue({ | ||
el: '#app', | ||
data: { | ||
person: { | ||
name: 'ark', | ||
age: 28, | ||
fav: 'computer' | ||
}, | ||
test: 'test', | ||
msg: '学习MVVM实现原理', | ||
htmlStr: '<p>双击666</p>', | ||
imgSrc: './imgs/mvvm.jpg' | ||
}, | ||
methods: { | ||
handlerClick () { | ||
console.log(this); | ||
this.$data.person.name = 'ARK'; | ||
// 把data用defineProperty重写get 和set方法后 | ||
this.msg = '学习MVVM'; | ||
this.person.age = 30; | ||
alert(this.person.name); | ||
} | ||
} | ||
}) | ||
</script> | ||
</body> | ||
</html> |