创建vue实例
传入一些options(对象类型) 包含:
el 该属性决定挂载哪个元素
data 该数据直接定义或来自网络。从服务器加载
<!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">
<h2>{{message}}</h2>
<h2>{{name}}</h2>
</div>
<script src="./vue.js"></script>
<script>
// 编程范式 声明式编程
const app = new Vue({
el : '#app', //用于挂载要管理的元素
data: {// 定义数据
message : 'nihao',
name : 'xr'
}
})
// 命令式编程
// 1.创建div元素设置id属性 2.定义一个变量message 3。把变量放在div中显示;
</script>
</body>
</html>
展示列表:
<!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">
<ul>
<li v-for="item in movies">{{item}}</li>
</ul>
</div>
<script src="./vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data : {
message : 'nih',
movies:['123','www','qqq','rr']
}
})
</script>
</body>
</html>
响应式 app.movies.push(‘xxx’)会自动添加
计数器展示
命令式:拿button元素,添加监听事件;
监听式:
<!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">
<h2>当前计数:{{counter}}</h2>
<button v-on:click = "counter++">+</button>
<button v-on:click="counter--">-</button>
</div>
<script src="./vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data : {
counter : 0,
}
})
</script>
</body>
</html>
click操作变多时:
<!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">
<h2>当前计数:{{counter}}</h2>
<!-- <button v-on:click = "counter++">+</button>
<button v-on:click="counter--">-</button> -->
<button v-on:click ="add">+</button>
<button v-on:click = "sub">-</button>
</div>
<script src="./vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data : {
counter : 0,
},
methods: {
add : function () {
console.log('add被执行');
this.counter++;
},
sub : function () {
console.log('ss');
this.counter--
}
}
})
</script>
</body>
</html>
注意 this.counter
新属性methods定义方法
@click: 语法糖 等于v-on:click