两个文件,一个html文件,一个js文件。
<body>
<div id="app">
<button @click="handlePrint">打印</button>
</div>
</body>
<script src="./Vue.js"></script>
<script>
new Vue({
el: '#app',
methods: {
handlePrint() {
console.log("我的发")
}
}
})
</script>
class Vue {
constructor(options) {
this.$options = options;
this.$el = document.querySelector(options.el);
// 编译模板
this.compile(this.$el);
}
compile(node) {
node.childNodes.forEach((item, index) => {
// 判断节点类型
// 1===元素节点
if (item.nodeType === 1) {
if (item.hasAttribute("@click")) {
let vmKey = item.getAttribute("@click").trim();
item.addEventListener('click', (event) => {