js编程范式:命令式编程
vue编程: 声明式编程
语法:
标签元素
声明变量(const/let)
<div id="app">{{message}}</div>
const app = new Vue({
el: '#app', //用于挂载要管理的元素
data: { //定义数据
message: 'hello zyx!'
}
})
<!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>Document</title>
</head>
<body>
<div id="app">
<h2>当前计数: {{counter}}</h2>
<button @click="increment">+</button>
<button @click="decrement">-</button>
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
counter: 0
},
methods: {
increment() {
this.counter++,
console.log('被执行了' + this.counter)
},
decrement() {
this.counter--,
console.log("执行了" + this.counter)
}
}
})
</script>
</body>
</html>
上述计数器的MVVM