<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>todolist</title>
</head>
<body>
<div id="app">
<p>{{name}}</p>
<p q-text="name"></p>
<p>{{age}}</p>
<p>{{doubleAge}}</p>
<input type="text" q-model="name"/>
<button @click="changeName">点击</button>
<div q-html="html"></div>
</div>
<script> class QVue {
constructor(options){
this.$options = options;
// 数据响应
this.$data = options.data || {};
// 监听数据变化
this.observe(this.$data);
// 主要用来解析各种指令,比如v-modal,v-on:click等指令
new Compile(options.el,this);
// 执行生命周期
if(options.created){
options.created.call(this);
}
}
// 观察数据变化
observe(value){
if(!value || typeof value !== "object"){
return;
}
let keys = Object.keys(value);
keys.forEach((key)=> {
this.defineReactive(value,key,value[key]);
// 代理data中的属性到vue实例上
this.proxyData(key);
})
}
// 代理Data
proxyData(key){
Object.defineProperty(this,key,{
get(){
return this.$data[key];
},
set(newVal){
this.$data[key] = newVal;
}
})
}
// 数据响应
defineReactive(obj,key,val){
// 解决数据层次嵌套
this.observe(val);
const dep = new Dep();
Object.defineProperty(obj, key,{
get(){
// 向管理watcher的对象追加watcher实例
// 方便管理
Dep.target && dep.appDep(Dep.target);
return val;
},
set(newVal){
if(newVal === val){
return;
}
val = newVal;
// console.log(`${key}更新了:${newVal}`)
dep.notify();
}
})
}
} // 管理watcher
class Dep {
constructor() {
// 存储
this.deps = [];
}
// 添加watcher
appDep(dep){
this.deps.push(dep);
}
// 通知所有的watcher进行更新
notify(){
this.deps.forEach((dep) => {
dep.update();
})
}
}
// 观察者 做具体更新
class Watcher {
constructor(vm,key,cb){
// Vue实例
this.vm = vm;
// 需要更新的key
this.key = key;
// 更新后执行的函数
this.cb = cb;
// 将当前watcher实例指定到Dep静态属性target
// 用来在类间进行通信
Dep.target = this;
// 触发getter,添加依赖
this.vm[this.key];
Dep.target = null;
}
update(){
this.cb.call(this.vm,this.vm[this.key]);
}
} class Compile {
constructor(el,vm) {
// 要遍历的宿主节点
this.$el = document.querySelector(el);
this.$vm = vm; // 编译
if(this.$el){
// 转换宿主节点内容为片段Fragment元素
this.$fragment = this.node2Fragment(this.$el);
// 执行编译过程
this.compile(this.$fragment);
// 将编译完的HTML结果追加至宿主节点中
this.$el.appendChild(this.$fragment);
}
} // 将宿主元素中代码片段取出来,遍历,这样做比较高效
node2Fragment(el){
const frag = document.createDocumentFragment();
// 将宿主元素中所有子元素**(搬家,搬家,搬家)**至frag中
let child;
// 如果 el.firstChild 为undefined或null则会停止循环
while(child = el.firstChild){
frag.appendChild(child);
}
return frag;
} compile(el){
// 宿主节点下的所有子元素
const childNodes = el.childNodes;
Array.from(childNodes).forEach((node) => {
if(this.isElement(node)){
// 如果是元素
// 拿到元素上所有的执行,伪数组
const nodeAttrs = node.attributes;
Array.from(nodeAttrs).forEach((attr) => {
console.log(attr)
// 属性名
const attrName = attr.name;
// 属性值
const exp = attr.value;
// 如果是指令
if(this.isDirective(attrName)){
// q-text
// 获取指令后面的内容
const dir = attrName.substring(2);
// 执行更新
this[dir] && this[dir](node,this.$vm,exp);
}
// 如果是事件
if(this.isEvent(attrName)){
// 事件处理
let dir = attrName.substring(1); // @
this.eventHandler(node,this.$vm,exp,dir);
}
})
}else if(this.isInterpolation(node)){
// 如果是插值文本
this.compileText(node);
//console.log("编译文本"+node.textContent)
}
// 递归子元素,解决元素嵌套问题
if(node.childNodes && node.childNodes.length){
this.compile(node);
}
})
}
// 是否为节点
isElement(node){
return node.nodeType === 1;
}
// 是否为插值文本
isInterpolation(node){
return node.nodeType === 3 && /\{\{(.*)\}\}/.test(node.textContent);
}
// 是否为指令
isDirective(attr){
return attr.indexOf("q-") == 0;
}
// 是否为事件
isEvent(attr){
return attr.indexOf("@") == 0;
} // v-text
text(node,vm,exp){
this.update( node, vm, exp, "text");
}
textUpdater(node,value){
node.textContent = value;
} // 双向绑定
// v-model
model(node,vm,exp){
// 指定input的value属性,模型到视图的绑定
this.update(node,vm,exp,"model");
// 试图对模型的响应
node.addEventListener('input',(e) => {
vm[exp] = e.target.value;
})
}
modelUpdater(node,value){
node.value = value;
} // v-html
html(node,vm,exp){
this.update(node,vm,exp,"html")
}
htmlUpdater(node,value){
node.innerHTML = value;
} // 更新插值文本
compileText(node){
let key = RegExp.$1;
this.update( node, this.$vm, key, "text");
}
// 事件处理器
eventHandler(node,vm,exp,dir){
let fn = vm.$options.methods && vm.$options.methods[exp];
if(dir && fn){
node.addEventListener(dir,fn.bind(vm));
}
} // 更新函数 - 桥接
update(node,vm,exp,dir){
const updateFn = this[`${dir}Updater`];
// 初始化
updateFn && updateFn(node,vm[exp]);
// 依赖收集
new Watcher(vm,exp,function(value){
updateFn && updateFn(node,value);
})
}
} new QVue({
el:"#app",
data:{
name:"I am test",
age:12,
html:"<button>这是一个后插入的按钮</button>"
},
created(){
//console.log("开始吧,QVue");
setTimeout(() => {
this.name = "测试数据,更改了么";
},2000)
},
methods:{
changeName(){
this.name = "点击啦,改变吧";
this.age = 1000000;
}
}
}) </script>
</body> </html>

转自:https://segmentfault.com/a/1190000018614946?utm_medium=referral&utm_source=tuicool

最新文章

  1. SQL Server 2008中的代码安全&lt;转&gt;
  2. 我的ORM汇总
  3. MVVM框架下,WPF实现Datagrid里的全选和选择
  4. LayaAir引擎——(十一)
  5. PHP 实现单一入口 apache配置
  6. cpu和内存的关系
  7. Ui设计哪里有好的素材
  8. Cocos移植到Android-Android.mk编译文件
  9. 多线程学习之三生产者消费者模式Guarded Suspension
  10. pwnable.kr input解题记录
  11. jquery.form插件 提交表单 type=&quot;hidden&quot;取不到值的问题记录
  12. 深入理解hashCode
  13. sql sever 2012重装数据库时,出现cannot find one or more components, Please reinstall the application.解决方法
  14. Windows平台dump文件的产生,调试;工程配置pdb文件怎么生成
  15. ALGO-17_蓝桥杯_算法训练_乘积最大(DP)
  16. [转载]windows下安装Python虚拟环境virtualenv,virtualenvwrapper-win
  17. [源][osg][osgBullet]osgBullet例子介绍
  18. XAML中格式化日期
  19. VueCli
  20. expdp全库备份rac数据库因错误终止

热门文章

  1. vue3和vue2的区别
  2. GMAC网卡相关介绍与分析
  3. [代码审计基础 02]-SQL注入和预编译和预编译绕过
  4. JZOJ 3184. 【GDOI2013模拟7】最大异或和
  5. EMBARK研究: 依那西普治疗早期nr-axSpA达48周的临床和MRI疗效
  6. js替换字符中指定所有字符
  7. Java/.Net双平台核心,Jvm和CLR运行异同点
  8. 说一下created和mounted区别
  9. Spring cloud Sleuth 分布式链路跟踪
  10. http 协议( 简单了解 )