常用操作指令:
v-bind
示例1:基本使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-bind</title>
</head>
<body>
<div id="app">
<img v-bind:src="imgUrl" alt="">
<a href="http://www.baidu.com">百度一下</a>
<a :href="aHref">百度一下</a>
</div>
</body>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
imgUrl: 'https://cn.vuejs.org/images/logo.png',
aHref: 'http://www.baidu.com'
}
})
</script>
</html>
其中‘:’是v-bind的语法糖
示例2:动态绑定class
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-bind动态绑定class</title>
<style>
.active {
color: red;
}
</style>
</head>
<body>
<div id="app">
<h2 :class="{active: isActive}">hello vue</h2>
<button @click="switchColor">切换字体颜色</button>
</div>
</body>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
isActive: true
},
methods: {
switchColor: function () {
this.isActive = !this.isActive
}
}
})
</script>
</html>
示例3:动态绑定style
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-bind动态绑定style</title>
</head>
<body>
<div id="app">
<h2 :style="{fontSize: finalSize + 'px',color: finalColor}">{{message}}</h2>
<button @click="add">+</button>
<button @click="sub">-</button>
</div>
</body>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: 'hello Vue',
finalSize: 100,
finalColor: 'green',
},
methods: {
add: function () {
this.finalSize++
},
sub: function () {
this.finalSize--
}
}
})
</script>
</html>
示例4:v-bind和v-for结合使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-bind与v-for</title>
<style>
.active {
color: red;
}
li {
cursor: pointer;
}
</style>
</head>
<body>
<div id="app">
<ul>
<li v-for="(item,index) in movies" :class="{active: index === currentIndex}" @click="liClick(index)">{{item}}</li>
</ul>
</div>
</body>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
movies: ['大话西游', '神话', '封神榜'],
currentIndex: 0
},
methods: {
liClick: function (index) {
this.currentIndex = index
}
}
})
</script>
</html>
v-on
示例1:基本使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-on的基本使用</title>
</head>
<body>
<div id="app">
<h2>{{counter}}</h2>
<button v-on:click="increment">+</button>
<button v-on:keyup.enter="decrement">-</button>
</div>
</body>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
counter: 0
},
methods: {
increment: function () {
this.counter++
},
decrement: function () {
this.counter--
}
}
})
</script>
</html>
示例2:参数问题
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-on的参数问题</title>
</head>
<body>
<div id="app">
<!-- 当无参数时,()是否添加无影响-->
<button @click="btnClick1">btnClick1-01</button>
<button @click="btnClick1()">btnClick1-02</button>
<br>
<!-- 在事件定义时,写方法时省略了小(),但是方法本身是需要一个参数的,这个时候Vue会默认将浏览器生产的event事件对象作为参数传入到方法-->
<button @click="btnClick2">btnClick2-01</button>
<!-- 不加参数时为undefined-->
<button @click="btnClick2()">btnClick2-02</button>
<button @click="btnClick2('a')">btnClick2-03</button>
<br>
<!-- 方法定义时,我们需要event对象,同时又需要其他参数-->
<!-- 在调用方法时,如何手动获取到浏览器参数的event对象:$event-->
<button @click="btnClick3">btnClick3-01</button>
<button @click="btnClick3('a','event')">btnClick3-02</button>
<button @click="btnClick3('a',$event)">btnClick3-03</button>
<button @click="btnClick3('a','event',$event)">btnClick3-04</button>
</div>
</body>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: 'hello vue'
},
methods: {
btnClick1: function () {
console.log('btnClick')
},
btnClick2: function (temp) {
console.log(temp, 'btnClick2')
},
btnClick3: function (temp, event) {
console.log(temp, event)
}
}
})
</script>
</html>
示例3:修饰符的使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-on的修饰符</title>
</head>
<body>
<div id="app">
<!--1.stop修饰符-->
<div @click="divClick">
aaa
<!-- 阻止了事件冒泡-->
<button @click="btnClick">btn1</button>
<button @click.stop="btnClick">btn2</button>
</div>
<br>
<!--2.prevent修饰符-->
<form action="baidu">
<input type="submit" value="提交" @click="submitClick">
<input type="submit" value="提交2" @click.prevent="submitClick">
</form>
<!-- 3.监听某个键盘的键帽-->
<input type="text" @keyup="keyUp">
<input type="text" @keyup.enter="keyUp">
<!-- 4.once修饰符的使用-->
<button @click.once="onceClick">once</button>
</div>
</body>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
methods: {
divClick: function () {
console.log('dicClick');
},
btnClick: function () {
console.log('btnClick');
},
submitClick: function () {
console.log('submitClick');
},
keyUp: function () {
console.log('keyUp');
},
onceClick: function () {
console.log('onceClick')
}
}
})
</script>
</html>
v-if
示例1:v-if的基本使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-if</title>
</head>
<body>
<div id="app">
<h2 v-if="message">{{message}}</h2>
</div>
</body>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: true
}
})
</script>
</html>
示例2:v-if和v-else
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-if</title>
</head>
<body>
<div id="app">
<h2 v-if="message">{{message}}</h2>
</div>
</body>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: true
}
})
</script>
</html>
示例3:v-else-if
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-else-if</title>
</head>
<body>
<div id="app">
<h2 v-if="score<60"><60</h2>
<h2 v-else-if="score<80"><80</h2>
<h2 v-else>>80</h2>
</div>
</body>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
score: 0
}
})
</script>
</html>
v-for
示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-for遍历数组和对象</title>
</head>
<body>
<div id="app">
<!-- 遍历数组-->
<h2 v-for="item in list"> {{item}}</h2>
<h2 v-for="(item,index) in list">{{item}}-{{index}}</h2>
<!-- 遍历对象-->
<h2 v-for="value in people">{{value}}</h2>
<h2 v-for="(value,name) in people">{{value}}-{{name}}</h2>
<h2 v-for="(value,name,index) in people">{{value}}-{{name}}-{{index}}</h2>
</div>
</body>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
list: ['A', 'B', 'C'],
people: {
name: 'hyhy',
age: 18,
gender: 'male'
}
}
})
</script>
</html>
可响应式的数组方法
pop()
shift()
unshift
splice()
sort()
reverse()
v-model
示例1:v-model的基本使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-model的基本使用</title>
</head>
<body>
<div id="app">
<input type="text" value="message" v-model="message">
<h2>{{message}}</h2>
</div>
</body>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: 'hello Vue'
}
})
</script>
</html>
v-model主要用于实现表单的双向绑定
例如:
- 在input框中改动,真实的message也会发生变化。
- 同样在控制台中改变message的值,input框和h2标签中的内容同样会发生改变。
示例2:v-model的原理
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-model的原理</title>
</head>
<body>
<div id="app">
<input type="text" :value="message" @input="valueChange($event)">
<h2>{{message}}</h2>
</div>
</body>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: 'Hello Vue'
},
methods: {
valueChange: function (event) {
this.message = event.target.value
}
}
})
</script>
</html>
v-model其实是一个语法糖,它背后的本质上是包含两个操作:
- v-bind绑定一个value的属性
- v-on指令给当前元素绑定input事件
上述两段代码的效果一致
v-model的几种使用实例
示例1: v-model与radio的结合使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-model与radio结合</title>
</head>
<body>
<div id="app">
<label for="male">
<!-- 使用v-model时radio中的name可以省略-->
<input type="radio" id="male" v-model="gender" value="男">男
</label>
<label for="female">
<input type="radio" id="female" v-model="gender" value="女">女
</label>
<h2>{{gender}}</h2>
</div>
</body>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
gender: '男'
}
})
</script>
</html>
示例2:v-model与CheckBox的结合使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-mode与CheckBox的结合</title>
</head>
<body>
<div id="app">
<!-- 单选框-->
<label for="licence">
<input type="checkbox" id="licence" v-model="isAgree">同意协议
</label>
<h2>isAgree:{{isAgree}}</h2>
<button :disabled="!isAgree">下一步</button>
<br>
<br>
<br>
<!-- 多选框-->
<input type="checkbox" value="篮球" v-model="hobbies">篮球
<input type="checkbox" value="足球" v-model="hobbies">足球
<input type="checkbox" value="羽毛球" v-model="hobbies">羽毛球
<input type="checkbox" value="乒乓球" v-model="hobbies">乒乓球
<h2>hobbies: {{hobbies}}</h2>
</div>
</body>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: "#app",
data: {
isAgree: false,
hobbies: []
}
})
</script>
</html>
示例3:v-model与checkbox结合-改
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-mode与CheckBox的结合-改</title>
</head>
<body>
<div id="app">
<!-- 多选框-->
<label v-for="item in selectList" :for="item">
<input type="checkbox" :value="item" v-model="hobbies">{{item}}
</label>
<h2>hobbies: {{hobbies}}</h2>
</div>
</body>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: "#app",
data: {
isAgree: false,
hobbies: [],
selectList: ['篮球', '足球', '羽毛球', '乒乓球']
}
})
</script>
</html>
示例4:v-model与select的结合
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-model结合select</title>
</head>
<body>
<div id="app">
<!-- 单选-->
<select name="fruit" v-model="fruit">
<option value="苹果">苹果</option>
<option value="香蕉">香蕉</option>
<option value="榴莲">榴莲</option>
<option value="橙子">橙子</option>
</select>
<h2>选择的水果是: {{fruit}}</h2>
<br>
<br>
<br>
<!-- 多选-->
<!-- 可以通过ctrl选中多个-->
<select name="fruit" v-model="fruits" multiple>
<option value="苹果">苹果</option>
<option value="香蕉">香蕉</option>
<option value="榴莲">榴莲</option>
<option value="橙子">橙子</option>
</select>
<h2>选择的水果是: {{fruits}}</h2>
</div>
</body>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
fruit: '苹果',
fruits: []
}
})
</script>
</html>
v-model的几个修饰符
示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-model的几个修饰符</title>
</head>
<body>
<div id="app">
<!-- 1.lazy修饰符:让数据在市区焦点或回车时才更新-->
<input type="text" v-model.lazy="message">
<h2>message: {{message}}</h2>
<br>
<br>
<!-- 2.number修饰符 :让输入框的内容自动转换为数字类型:-->
<input type="text" v-model.number="numberMessage">
<h2>numberMessage: {{numberMessage}}</h2>
<h2>type: {{typeof numberMessage}}</h2>
<br>
<br>
<!-- 3.trim修饰符:过来内容左右两边的空格-->
<input type="text" v-model.trim="trimMessage">
</div>
</body>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: '',
numberMessage: '',
trimMessage: ''
}
})
</script>
</html>