>[warning]v-on綁定事件
****
1. 思路, 還是解析標簽的屬性, 如果有v-on提取事件名字, 然后底層利用addEventListener綁定對應事件
代碼如下:
~~~
<script>
Compile.prototype = {
nodeToFragment(el) {
let fragment = document.createDocumentFragment();
let child = el.firstChild;
while (child) {
fragment.appendChild(child);
child = el.firstChild;
}
return fragment;
},
compileElement(el) {
let childNodes = el.childNodes;
let self = this;
[].slice.call(childNodes).forEach(function (node) {
let reg = /\{\{(.*)\}\}/;
let text = node.textContent;
if (self.isElementNode(node)) {
self.compile(node);
} else if (self.isTextNode(node) && reg.test(text)) {
self.compileText(node, reg.exec(text)[1]);
}
if (node.childNodes && node.childNodes.length) {
self.compileElement(node);
}
})
},
compileText(node, exp) {
let self = this;
let initText = this.vm[exp];
self.updateText(node, initText);
new Watcher(this.vm, function (val) {
self.updateText(node, val);
}, exp);
},
updateText(node, value) {
node.textContent = typeof value === 'undefined' ? '' : value;
},
isTextNode: function (node) {
return node.nodeType == 3;
},
isElementNode: function (node) {
return node.nodeType == 1;
},
compile(node) {
let nodeAttrs = node.attributes;
let self = this;
Array.prototype.forEach.call(nodeAttrs, function (attr) {
let attrName = attr.name;
if (self.isDirective(attrName)) {
let exp = attr.value;
let dir = attrName.substring(2);
// 4. 判斷是否是事件指令
if (self.isEventDirective(dir)) {
// 6. 給事件進行解析
self.compileEvent(node, self.vm, exp, dir);
} else if (self.isModelDirective(dir)) {
self.compileModel(node, self.vm, exp);
}
node.removeAttribute(attrName);
}
});
},
isDirective: function (attr) {
return attr.indexOf('v-') == 0;
},
isModelDirective: function (dir) {
return dir.indexOf('model') === 0;
},
compileModel: function (node, vm, exp) {
let self = this;
let val = this.vm[exp];
this.modelUpdater(node, val);
new Watcher(this.vm, function (value) {
self.modelUpdater(node, value);
}, exp);
node.addEventListener('input', function (e) {
let newValue = e.target.value;
if (val === newValue) {
return;
}
self.vm[exp] = newValue;
val = newValue;
});
},
modelUpdater: function (node, value) {
node.value = typeof value == 'undefined' ? '' : value;
},
// 5. 判斷是否以on:開頭的字符串
isEventDirective: function(dir) {
return dir.indexOf('on:') === 0;
},
// 7, 事件指令解析
// DOM標簽, vue對象, exp是key值, dir是事件名
compileEvent: function (node, vm, exp, dir) {
// 截取:后面的事件名字(例click)
let eventType = dir.split(':')[1];
// 獲取vue實例中的methods對應的對象, 然后拿到對應的方法體
let cb = vm.methods && vm.methods[exp];
if (eventType && cb) {
// 給這個DOM注冊事件, eventType事件觸發, 調用此方法, 傳入vue實例對象進去(這就是你為什么在methods中可以直接使用this代表vue對象的原因)
node.addEventListener(eventType, cb.bind(vm), false);
}
},
};
function Vue(options, exp) {
this.data = options.data();
// 3. 綁定methods
this.methods = options.methods;
let self = this;
Object.keys(this.data).forEach(function (key) {
self.proxyKeys(key);
});
observe(this.data);
new Compile(options.el, this);
options.mounted.call(this);
}
Vue.prototype = {
proxyKeys: function (key) {
let self = this;
Object.defineProperty(this, key, {
get: function () {
return self.data[key];
},
set: function (newVal) {
self.data[key] = newVal
}
})
}
}
</script>
<div id="app">
<h1>{{userName}}</h1>
<input type="text" v-model="userName">
<!--1. 使用v-on綁定click事件 -->
<button v-on:click="btn">點我試試</button>
</div>
<script>
new Vue({
el: "#app",
data() {
return {
"userName": "hello VueJS"
}
},
mounted() {
setTimeout(() => {
this.userName = "漂亮";
}, 2000);
},
// 2. 注冊methods
methods: {
btn() {
this.userName = "點按鈕了";
}
}
});
</script>
~~~
[當前頁源代碼](https://github.com/lidongxuwork126com/ldx_vue/tree/master/%E4%BB%BFVue%E6%BA%90%E7%A0%81)
- web前端
- CSS問題
- 布局
- 雙飛翼布局_flex方式
- 雙飛翼布局_margin方式
- 圣杯布局_flex方式
- 圣杯布局_margin方式
- 子元素居中問題
- 彈性布局
- 概念_
- 標準模式與混雜模式
- 各種FC
- line-height
- vertical-align
- CSS3新特性
- 效果
- div添加箭頭
- CSS繪制三角形
- JavaScript
- 兼容
- 事件相關
- 原理
- Ajax原理
- 繼承原理
- 原型鏈繼承
- 組合繼承
- 寄生組合繼承
- 數據綁定
- 1單向數據綁定m到c到v
- 2偽雙向數據綁定
- 3雙向數據綁定
- socket.io
- 運行時
- this指向
- 0.1+0.2問題
- 對象/數組-深拷貝&淺拷貝
- 事件循環
- typeof
- instanceof
- 概念
- 閉包
- 回調函數
- Promise
- 原生對象
- Attribute和property區別
- 防抖函數
- 節流函數
- 語言類型
- Vue
- Vue優缺點
- 仿Vue源碼
- 1數據綁定_Observe
- 2數據綁定_訂閱者&觀察者定義
- 3數據綁定_Vue類實現
- 4數據綁定_Vue訪問data更改
- 5DOM編譯_Compile_雙大括號模板講解
- 6DOM編譯_v-model講解
- 7DOM編譯_v-on:事件綁定講解
- 項目總結
- 使用Svg圖標
- vueCli環境_真機測試
- vueCli集成環信SDK
- 父子組件雙向綁定
- React
- React優缺點
- 我的組件庫
- Vue的組件庫
- 環信_聊天組件
- 面試題
- HTML_分類
- CSS_分類
- JavaScript_分類
- VueJS_分類
- ReactJS_分類
- AngularJS_分類
- 瀏覽器端
- 筆試題
- CSS
- 特殊布局
- JavaScript_
- 經典_宏任務_微任務
- 瀏覽器問題
- CORS
- web服務器
- Apache
- 開啟跨域
- Nginx
- 常用命令
- 正向代理
- 反向代理
- 負載均衡
- mac安裝Nginx
- 配置80端口
- 算法
- 冒泡排序
- 選擇排序
- 合并對象_排序
- 楊輝三角
- 紅黑樹
- 計算機基礎
- 網絡相關
- OSI七層模型
- http協議
- http工作原理
- https協議
- GET和POST區別
- hosts文件
- php相關
- session機制
- Linux
- 阿里云服務器
- linux使用Mysql
- 安裝mysql
- 導入.sql文件
- 遠程連接mysql
- linux使用xampp
- 安裝Xampp
- 配置web訪問
- 域名綁定服務器
- linux搭建git服務器_apache下
- 代碼管理
- 什么是git
- 命令行_使用git
- .gitignore文件講解
- 軟件
- VSCode的安裝
- 理財
- 基金
- 攝影