基本使用
<!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">{{message}}</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
const VM = new Vue({
el: '#app',
data() {
return {
message: 'Hello'
}
}
})
</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">
<h1 v-if="flag">{{message}}</h1>
<h1 v-show="flag">{{message}}</h1>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
const VM = new Vue({
el: '#app',
data() {
return {
message: 'Hello',
flag: true
}
}
})
</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 person">{{item}}</li>
</ul>
<ul>
<li v-for="item, key in student">{{key}} {{item.id}} {{item.name}} {{item.age}}</li>
</ul>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
const VM = new Vue({
el: '#app',
data() {
return {
person: ['小明', '小红', '小黑'],
student: [
{
id:001,
name: '小明',
age:18
},
{
id:002,
name: '小红',
age:17
},
{
id:003,
name: '小黑',
age:21
},
]
}
}
})
</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">
<img v-bind:src=imgUrl width="200px" height="200px">
<img :src=imgUrl width="200px" height="200px">
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
const VM = new Vue({
el: '#app',
data() {
return {
imgUrl: 'https://cn.vuejs.org/images/logo.svg',
}
},
})
</script>
</body>
</html>
绑定class
<!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">
<style>
.active {
background-color: orangered;
}
</style>
<title>Document</title>
</head>
<body>
<div id="app">
<h1 :class="{active:isTrue}">{{message}}</h1>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
const VM = new Vue({
el: '#app',
data() {
return {
message: 'Hello',
isTrue: true
}
},
})
</script>
</body>
</html>
绑定style
<!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">
<div :style="styleObj"></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
const VM = new Vue({
el: '#app',
data() {
return {
styleObj: {
width: '200px',
height: '200px',
'background-color': '#ff3300'
}
}
}
})
</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">
<p>用户名:{{username}}</p>
<input type="text" v-model="username">
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
const VM = new Vue({
el: '#app',
data() {
return {
username: '张三'
}
},
})
</script>
</body>
</html>
v-model的修饰符
<!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">
<p>{{num}}</p>
<input type="text" v-model.number="num">
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
const VM = new Vue({
el: '#app',
data() {
return {
num: 0
}
},
})
</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">
<p>count的值为:{{count}}</p>
<button v-on:click="add">+1</button>
<button @click="sub">-1</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
const VM = new Vue({
el: '#app',
data() {
return {
count: 0
}
},
methods: {
add: function () {
this.count +=1
},
sub() {
this.count -=1
}
}
})
</script>
</body>
</html>
$event事件对象
<!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">
<button @click="fn1">button</button>
<br>
<button @click="fn2(1,$event)">button</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
const VM = new Vue({
el: '#app',
data() {
return {
}
},
methods: {
fn1(e) {
console.log(e);
},
fn2(num,e){
console.log(e)
}
}
})
</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">
<a href="http://www.google.com" @click.prevent="fn">Google</a>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
const VM = new Vue({
el: '#app',
data() {
return {
}
},
methods: {
fn(e) {
}
}
})
</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">
<input type="text" @keyup.esc="clear">
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
const VM = new Vue({
el: '#app',
data() {
return {
}
},
methods: {
clear(e) {
console.log('已触发清除方法');
e.target.value = '';
}
}
})
</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">
姓:<input type="text" v-model="firstName">
名:<input type="text" v-model="lastName">
<br>
<p>姓名:{{fullName}}</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
const VM = new Vue({
el: '#app',
data() {
return {
firstName: '张',
lastName: '三'
}
},
computed: {
fullName() {
return this.firstName + this.lastName;
}
}
})
</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">
<input type="text" v-model="msg">
<input type="text" v-model="test">
<input type="text" v-model="info.message">
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
const VM = new Vue({
el: '#app',
data() {
return {
msg: 'hello',
test: 'hello',
info: {
message: 'hello'
}
}
},
watch: {
msg(newVal, oldVal) {
console.log('msg值已发生变化');
console.log('新值' + newVal);
console.log('旧值' + oldVal);
},
test: {
handler(newVal, oldVal) {
console.log(newVal, oldVal);
},
immediate: true,
},
info: {
handler(newVal, oldVal) {
console.log(newVal, oldVal);
},
deep: true
}
}
})
</script>
</body>
</html>