案例
基本案例
1、初体验
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">{{message}}</div>
<script src="../js/vue.js"></script>
<script>
const app =new Vue({
el:'#app',
data:{
message:"测试"
}
})
</script>
</body>
</html>
2、数组展示
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id ="app">
<ul>
<li v-for="item in subject">{{item}} </li>
</ul>
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data: {
message: '你好啊',
subject: ['语文', '数学', '英语']
}
})
</script>
</body>
</html>
3、v-on 计数使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<h2>当前计数器:{{counter}}</h2>
<button v-on:click="counter++">+</button>
<button v-on:click="sub">-</button>
<button @click="sub">-</button>
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el:"#app",
data:{
counter:0
},
methods:{
sub:function(){
this.counter--;
}
}
})
</script>
</body>
</html>
4、mustacher语法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<h2>{{message}}</h2>
<h2>{{name + sex}}</h2>
<h2>{{name + '' + sex}}</h2>
<h2>{{name}}{{sex}}</h2>
<h2>{{age>10?'大于10':'不大于10'}}</h2>
</div>
<script src="../js/vue.js"></script>
<script>
const app =new Vue({
el:'#app',
data:{
message:"测试",
name:"张三",
sex:"男",
age:10
}
})
</script>
</body>
</html>
5、v-once
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<h2>{{message}}</h2>
<h2 v-once>{{message}}</h2>
</div>
<script src="../js/vue.js"></script>
<script>
const app =new Vue({
el:'#app',
data:{
message:"测试"
}
})
</script>
</body>
</html>
6、v-html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<h2>{{url}}</h2>
<h2 v-html="url"></h2>
</div>
<script src="../js/vue.js"></script>
<script>
const app =new Vue({
el:'#app',
data:{
message:"测试",
url:'<a href="http://www.baidu.com">百度一下</a>'
}
})
</script>
</body>
</html>
v-bind
1、基本用法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<!--错误的用法-->
<!--<img src="{{imgURL}}">-->
<img v-bind:src="imgURL">
<a v-bind:href="ahref">百度一下</a>
<!--语法糖写法
<img :src="imgURL">
<a :href="ahref">百度一下</a>-->
</div>
<script src="../js/vue.js"></script>
<script>
const app =new Vue({
el:'#app',
data:{
imgURL:"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3228549874,2173006364&fm=26&gp=0.jpg",
ahref:"http://www.baidu.com"
}
})
</script>
</body>
</html>
2.v-bind-class
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<style>
.activeClass{
color:red;
}
</style>
<div id="app">
<h2 class="activeClass">{{message}}</h2>
<h2 :class="active">{{message}}</h2>
<h2 :class="{activeClass:true}">{{message}}</h2>
<h2 :class="{activeClass:isActive}">{{message}}</h2>
<button @click="btnClick">改变颜色</button>
<h2 :class="getClass()">{{message}}</h2>
</div>
<script src="../js/vue.js"></script>
<script>
const app =new Vue({
el:'#app',
data:{
message:"测试",
active:'activeClass',
isActive:true
},
methods:{
btnClick:function(){
this.isActive=!this.isActive;
},
getClass:function(){
return {activeClass:this.isActive};
}
}
})
</script>
</body>
</html>
3、v-bind-style
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<!--<h2 :style="{key(css属性名):vaule(属性值) }">{{message}}</h2>-->
<h2 :style="{fontSize:'50px'}">{{message}}</h2>
<h2 :style="{fontSize:fontSize+'px'}">{{message}}</h2>
</div>
<script src="../js/vue.js"></script>
<script>
const app =new Vue({
el:'#app',
data:{
message:"测试",
fontSize:100
}
})
</script>
</body>
</html>
计算属性
conputed
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<h2>{{message}}</h2>
<h2>{{age}}</h2>
<h2>{{fullMessage}}</h2>
<h2>{{totalPrice}}</h2>
</div>
<script src="../js/vue.js"></script>
<script>
const app =new Vue({
el:'#app',
data:{
message:"测试",
age:10,
books:[
{name:'编程之美',price:10},
{name:'数据结构',price:20}
]
},
computed:{
fullMessage:function(){
return this.message+this.age;
},
totalPrice:function(){
let result = 0;
for(let i = 0;i<this.books.length;i++){
result += this.books[i].price;
}
return result;
}
}
})
</script>
</body>
</html>
computed和methods的区别
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<h2>{{message}}</h2>
<h2>{{age}}</h2>
<h2>{{getFullMessage()}}</h2>
<h2>{{getFullMessage()}}</h2>
<h2>{{fullMessage}}</h2>
<h2>{{fullMessage}}</h2>
</div>
<script src="../js/vue.js"></script>
<script>
const app =new Vue({
el:'#app',
data:{
message:"测试",
age:10,
books:[
{name:'编程之美',price:10},
{name:'数据结构',price:20}
]
},
computed:{
fullMessage:function(){
console.log("computed")
return this.message+this.age;
}
},
methods:{
getFullMessage:function(){
console.log("methods")
return this.message+this.age;
}
}
})
</script>
</body>
</html>
es6语法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<button>按钮1</button>
<button>按钮2</button>
<button>按钮3</button>
<button>按钮4</button>
<button>按钮5</button>
<script src="../js/vue.js"></script>
<script>
var btns = document.getElementsByTagName('button');
for(var i=0;i<btns.length;i++ ){
btns[i].addEventListener('click',function(){
console.log("第"+i+"个按钮被点击")
})
}
/*for(let i=0;i<btns.length;i++ ){
btns[i].addEventListener('click',function(){
console.log("第"+i+"个按钮被点击")
})
}*/
/*使用闭包解决var无块级作用域问题*/
/*for(var i=0;i<btns.length;i++ ){
(function(i){
btns[i].addEventListener('click',function(){
console.log("第"+i+"个按钮被点击")
})
})(i)
}*/
</script>
</body>
</html>
v-on
阻止冒泡.stop(相当于event.stopPropagation)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<div @click="divClick">
测试
<!--<button @click="btnClick">
按钮
</button>-->
<!--阻止冒泡-->
<button @click.stop ="btnClick">
按钮
</button>
</div>
<br>
<form action="submit">
<input type="submit" value="提交" @click.prevent="submitClick">
</form>
<input type="text" @keyup.enter="keyUp">
<button @click.once="btn2Click">按钮2</button>
</div>
<script src="../js/vue.js"></script>
<script>
const app =new Vue({
el:'#app',
data:{
message:"测试"
},
methods:{
divClick(){
console.log("divClick")
},
btnClick(){
console.log("btnClick")
},
submitClick(){
console.log("submitClick")
},
keyUp(){
console.log("keyUp")
},
btn2Click(){
console.log("btn2Click")
}
}
})
</script>
</body>
</html>
无.stop点击按钮
加上.stop点击按钮
.prevent(form不会直接提交)
keyup(监听键盘点击时间)
.once(按钮只能点击一次)
v-if
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<h2 v-if="isShow==0">isShow为0</h2>
<h2 v-else-if="isShow==1">Show为1</h2>
<h2 v-else>Show为其他</h2>
{{result}}
</div>
<script src="../js/vue.js"></script>
<script>
const app =new Vue({
el:'#app',
data:{
message:"测试",
isShow:1
},
computed:{
result(){
let showMessag ="";
if(this.isShow ==0){
showMessag="isShow为0"
}else if(this.isShow==1){
showMessag="isShow为1"
}else{
showMessag="isShow为其他"
}
return showMessag
}
}
})
</script>
</body>
</html>
input需要加key,vue中虚拟dom会复用input框
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<span v-if="isUser">
<label for="userName">用户账号</label>
<input type="text" id="userName" placeholder="用户账号">
</span>
<span v-else>
<label for="email">用户邮箱</label>
<input type="text" id="email" placeholder="用户邮箱">
</span>
<span v-if="isUser">
<label for="userName">用户账号</label>
<input type="text" id="userName1" placeholder="用户账号" key="userName1">
</span>
<span v-else>
<label for="email">用户邮箱</label>
<input type="text" id="email1" placeholder="用户邮箱" key="">
</span>
<button @click="isUser=!isUser">切换</button>
</div>
<script src="../js/vue.js"></script>
<script>
const app =new Vue({
el:'#app',
data:{
isUser:true
}
})
</script>
</body>
</html>
v-for
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<ul>
<li v-for="item in names">{{item}}</li>
</ul>
<ul>
<li v-for="(item,index) in names">{{index+1}}、{{item}}</li>
</ul>
<ul>
<li v-for="item in info">{{item}}</li>
</ul>
<ul>
<li v-for="(value,key) in info">{{key}}--{{value}}</li>
</ul>
</div>
<script src="../js/vue.js"></script>
<script>
const app =new Vue({
el:'#app',
data:{
names:["张三","李四","王五"],
info:{
name:'张三',
age:10,
sex:'男'
}
}
})
</script>
</body>
</html>
数组操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<ul>
<li v-for="item in names">{{item}}</li>
</ul>
<button @click="btnClick">按钮</button>
<button @click="pushClick">push</button>
<button @click="popClick">pop</button>
<button @click="shiftClick">shift</button>
<button @click="unshiftClick">unshift</button>
<button @click="spliceClick">splice</button>
<button @click="sortClick">sort</button>
<button @click="reverseClick">reverse</button>
</div>
<script src="../js/vue.js"></script>
<script>
const app =new Vue({
el:'#app',
data:{
names:["张三","李四","王五"]
},
methods:{
btnClick(){
/*不是响应式的*/
this.names[0]="ssss";
},
pushClick(){
this.names.push("赵六");
},
popClick(){
/*删除最后一个元素*/
this.names.pop();
},
shiftClick(){
/*删除第一个元素*/
this.names.shift();
},
unshiftClick(){
this.names.unshift("孙二");
},
spliceClick(){
/*删除元素*/
this.names.splice(2,1);
/*删除一个元素,并添加一个元素*/
this.names.splice(1,1,'小三');
},
sortClick(){
this.names.sort();
},
reverseClick(){
this.names.reverse();
},
}
})
</script>
</body>
</html>
点击数组变红案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.active{
color:red
}
</style>
</head>
<body>
<div id="app">
<ul>
<li v-for="(item,index) in names" :class="{active:currentIndex == index}" @click="liClick(index)">{{item}}</li>
</ul>
</div>
<script src="../js/vue.js"></script>
<script>
const app =new Vue({
el:'#app',
data:{
names:["张三","李四","王五"],
isActive:true,
currentIndex:0
},
methods:{
liClick(index){
this.currentIndex=index
}
}
})
</script>
</body>
</html>
购物车练习
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
table{
border: 1px solid #e9e9e9;
border-collapse: collapse;
border-spacing: 0;
}
th,td{
padding: 8px 16px;
border: 1px solid #e9e9e9;
text-align: left;
}
th{
background-color:#f7f7f7 ;
color: #5566cc;
font-weight: 600;
}
</style>
</head>
<body>
<div id="app">
<div v-if="books.length">
<table>
<thead>
<tr>
<th></th>
<th>书籍名称</th>
<th>出版日期</th>
<th>价格</th>
<th>购买数量</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="(item,index) in books">
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td>{{item.date}}</td>
<!--<td>{{getFinalPrice(item.price)}}</td>-->
<td>{{item.price | showPrice}}</td>
<td>
<button @click="decrement(index)" v-bind:disabled="item.count<=1">-</button>
{{item.count}}
<button @click="increment(index) ">+</button>
</td>
<td>
<button @click="removeHandle(index)">移除</button>
</td>
</tr>
</tbody>
</table>
<h2>总价格{{totalPrice|showPrice}}</h2>
</div>
<h2 v-else>购物车为空</h2>
</div>
<script src="../js/vue.js"></script>
<script>
const app =new Vue({
el:'#app',
data:{
books:[
{
id:1,
name:"高数",
date:"2008-9",
price:35,
count:1
},
{
id:2,
name:"线性代数",
date:"2018-9",
price:39,
count:1
},
{
id:3,
name:"政治",
date:"2008-3",
price:34,
count:1
},
{
id:4,
name:"历史",
date:"2004-9",
price:32,
count:1
}
]
},
methods:{
getFinalPrice(price){
return '¥'+price.toFixed(2)
},
increment(index){
this.books[index].count++;
},
decrement(index){
this.books[index].count--;
},
removeHandle(index){
this.books.splice(index,1)
}
},
computed:{
totalPrice(){
let totalPrice=0;
/*for(let i=0;i<this.books.length;i++){
totalPrice += this.books[i].price * this.books[i].count;
}*/
/*for(let i in this.books){
totalPrice += this.books[i].price * this.books[i].count;
}*/
for(let item of this.books){
totalPrice += item.price * item.count;
}
return totalPrice
}
},
filters:{
showPrice(price){
return '¥'+price.toFixed(2)
}
}
})
</script>
</body>
</html>
高阶函数练习
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
const nums=[10,20,100,200,400,40,50]
//1.需求,取出所有小于100的数字
let newNums=[]
for(let n of nums){
if(n<100){
newNums.push(n)
}
}
console.log(newNums)
//2.需求,将所有小于100的数字乘以2
let new2Nums=[]
for(let n of newNums){
new2Nums.push(n*2)
}
console.log(new2Nums)
/*filter回调函数必须返回boolean值,放返回true,函数内部会自动将这次回调加入到新的数组中,返回false,会过滤这次结果*/
let newNums1= nums.filter(function (n) {
return n<100
})
console.log(newNums1)
let new2Nums1 = newNums1.map(function (n) {
return n*2
})
console.log(new2Nums1)
let total = new2Nums1.reduce(function (prevalue,n) {
return prevalue+n
},0)
console.log(total)
</script>
</body>
</html>
v-model
v-model-text
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<input type="text" :value="message" @input="message = $event.target.value">
<input type="text" v-model="message">
{{message}}
</div>
<script src="../js/vue.js"></script>
<script>
const app =new Vue({
el:'#app',
data:{
message:"测试"
}
})
</script>
</body>
</html>
v-model-radio
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<label for="male">
<input type="radio" id = "male" name="sex" value="男" v-model="sex">男
</label>
<label for="female">
<input type="radio" id = "female" name="sex" value="女" v-model="sex">女
</label>
<h2>您选择的性别是{{sex}}</h2>
</div>
<script src="../js/vue.js"></script>
<script>
const app =new Vue({
el:'#app',
data:{
message:"测试",
sex:""
}
})
</script>
</body>
</html>
v-model-checkbox
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<label for="agree">
<input type="checkbox" id = "agree" v-model="isAgree">同意
</label>
<h2>您选择是{{isAgree}}</h2>
<button :disabled="!isAgree">下一步</button>
<br>
<input type="checkbox" value="篮球" v-model="hobbies">篮球
<input type="checkbox" value="足球" v-model="hobbies">足球
<input type="checkbox" value="羽毛球" v-model="hobbies">羽毛球
<h2>您的爱好是{{hobbies}}</h2>
<label v-for="item of originHobbies" :for="item">
<input type="checkbox" :value="item" :id="item" v-model="hobbies">{{item}}
</label>
</div>
<script src="../js/vue.js"></script>
<script>
const app =new Vue({
el:'#app',
data:{
message:"测试",
isAgree:false,
hobbies:[],
originHobbies:["篮球","足球","排球","羽毛球"]
}
})
</script>
</body>
</html>
v-model-select
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<select v-model="fruit">
<option value="苹果" >苹果</option>
<option value="香蕉" >香蕉</option>
<option value="梨" >梨</option>
</select>
<h2>您选择的水果是{{fruit}}</h2>
<select v-model="fruits" multiple>
<option value="苹果" >苹果</option>
<option value="香蕉" >香蕉</option>
<option value="梨" >梨</option>
</select>
<h2>您选择的水果是{{fruits}}</h2>
</div>
<script src="../js/vue.js"></script>
<script>
const app =new Vue({
el:'#app',
data:{
message:"测试",
fruit:"梨",
fruits:[]
}
})
</script>
</body>
</html>
v-model修饰符
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<!--输入的时候不会立刻改变,失焦的时候改变-->
<input type="text" v-model.lazy="message">
<h2>{{message}}</h2>
<!--只能输入数字-->
<input type="text" v-model.number="age">
<h2>{{age}}--{{typeof age}}</h2>
<!--去掉空格-->
<input type="text" v-model.trim="name">
<h2>您输入的字符{{name}}</h2>
</div>
<script src="../js/vue.js"></script>
<script>
const app =new Vue({
el:'#app',
data:{
message:"测试",
age:10,
name:""
}
})
</script>
</body>
</html>