<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div id="app">
{{ msg }}111{{ msg }}
<div>{{msg}}</div>
</div>
<script>
class Vue {
constructor(options) {
this.options = options
/* 初始化编译dom */
this.compile()
}
compile() {
let el = document.querySelector(this.options.el)
if (!el) return console.warn('element not exist!')
let childNodes = el.childNodes
/* 递归编译节点 */
childNodes.length && this.compileNodes(childNodes)
}
compileNodes(childNodes) {
/* 循环节点 */
childNodes.forEach(node => {
/* 如果是文本节点,则进行替换插值表达式 */
if (node.nodeType === 3) {
let reg = /\{\{\s*([^\{\{\}\}\s]+)\s*\}\}/g
if (reg.test(node.textContent)) {
/* $1就是插值表达式里面的属性值 */
let $1 = RegExp.$1
node.textContent = node.textContent.replace(reg, this.options.data[$1])
}
} else {
/* 如果不是文本节点,则进行递归,并且有子节点 */
node.childNodes.length && this.compileNodes(node.childNodes)
}
})
}
}
new Vue({
el: '#app',
data: {
msg: '测试数据'
}
})
</script>
</body>
</html>
评论 (0)