day01
01.第一个Vue实例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="box">
<p v-text="msg"></p>
<hr>
<p>姓名:{{username}}</p>
<p>性别:{{sex}}</p>
<hr>
<div v-text="info"></div>
<div>{{info}}</div>
<hr>
<div v-html="info"></div>
</div>
<script src="js/vue.js"></script>
<script type="text/javascript">
const vm=new Vue({
el:'#box',
data:{
msg:'hello vue',
username:'张三',
sex:'女',
info:'<h4 style="color: red;font-weight: bold">hello vue.js</h4>'
}
})
</script>
</body>
</html>
02.内容渲染指令
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="box">
<p v-text="msg"></p>
<hr>
<p>姓名:{{username}}</p>
<p>性别:{{sex}}</p>
<hr>
<div v-text="info"></div>
<div>{{info}}</div>
<hr>
<div v-html="info"></div>
</div>
<script src="js/vue.js"></script>
<script type="text/javascript">
const vm=new Vue({
el:'#box',
data:{
msg:'hello vue',
username:'张三',
sex:'女',
info:'<h4 style="color: red;font-weight: bold">hello vue.js</h4>'
}
})
</script>
</body>
</html>
03.属性绑定指令
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<input type="text" v-bind:placeholder="tips">
<hr>
<img v-bind:src="photo" style="width: 100px">
</div>
<script src="js/vue.js"></script>
<script>
const vm=new Vue({
el:'#app',
data:{
tips:'请输入用户名',
photo:'https://www.runoob.com/wp-content/uploads/2017/01/vue.png',
}
})
</script>
</body>
</html>
04.Javascript表达式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<h1>1+2的结果是{{1+2}}</h1>
{{number+1}}
<hr>
{{ok?'YES':'NO'}}
<hr>
{{msg}}的反转结果是:{{msg.split('').reverse().join('')}}
<hr>
<div :title="'box'+index">这是一个div</div>
<hr>
</div>
<script src="js/vue.js"></script>
<script>
const vm=new Vue({
el:'#app',
data:{
number:1,
ok:1,
msg:'hello vue',
index:3
}
})
</script>
</body>
</html>
05.事件绑定指令
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<h3>count的值为{{count}}</h3>
<button @click="addCount(2)">+</button>
<button @click="sub(1)">-</button>
</div>
<script src="js/vue.js"></script>
<script>
const vm=new Vue({
el:'#app',
data:{
count:0,
},
methods:{
addCount (n) {
this.count+=n
},
sub(n){
this.count-=n
}
}
})
</script>
</body>
</html>
06.事件对象$event
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<p>count的值是:{{count}}</p>
<button @click="add()">+n</button>
<button @click="add(1,$event)">+n</button>
</div>
<script src="js/vue.js"></script>
<script>
const vm=new Vue({
el:'#app',
data:{
count:0
},
methods:{
add(n,e){
this.count+=1
if(this.count%2==0){
e.target.style.backgroundColor='red'
}else {
e.target.style.backgroundColor='green'
}
}
}
})
</script>
</body>
</html>
07.事件修饰符
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<a href="http://www.baidu.com" @click.prevent="show">百度</a>
<hr>
<div style="height:150px;background-color:orangered;padding-left: 100px;line-height: 150px;" @click="divHandler">
<button @click="btnHandler">按钮</button>
</div>
</div>
<script src="js/vue.js"></script>
<script>
const vm=new Vue({
el:'#app',
data:{},
methods:{
show(e){
e.preventDefault()
console.log('点击了a链接')
},
divHandler(){
console.log('divHandler')
},
btnHandler(e){
e.stopPropagation()
console.log('btnHandler')
}
}
})
</script>
</body>
</html>
08.按键修饰符
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<input @keyup.esc="clearInput">
<input @keyup.enter="commitAjax">
</div>
<script src="js/vue.js"></script>
<script>
const vm=new Vue({
el:'#app',
data:{},
methods:{
clearInput(e){
console.log('触发了clearInput函数')
e.target.value=''
},
commitAjax(e){
console.log('触发了Ajax函数')
e.target.value="触发了Ajax函数"
}
}
})
</script>
</body>
</html>
09.双向绑定指令
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
用户名{{username}} <br>
<input type="text" v-model="username">
<hr>
<input type="text" :value="username"><br>
<select v-model="city">
<option value="">请选择城市</option>
<option value="1">北京</option>
<option value="2">上海</option>
<option value="3">天津</option>
<option value="4">重庆</option>
</select>
</div>
<script src="js/vue.js"></script>
<script>
const vm=new Vue({
el:'#app',
data:{
username:'zhangsan',
city:''
},
})
</script>
</body>
</html>
10.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.number="n1">+<input type="text" v-model.number="n2">=<span>{{n1+n2}}</span><br>
<input type="text" v-model.number="n1">-<input type="text" v-model.number="n2">=<span>{{n1-n2}}</span><br>
<input type="text" v-model.number="n1">*<input type="text" v-model.number="n2">=<span>{{n1*n2}}</span><br>
<input type="text" v-model.number="n1">/<input type="text" v-model.number="n2">=<span>{{n1/n2}}</span><br>
<input type="text" v-model.trim="username">
<button @click="showName()">获取用户名</button>
<hr>
<input type="text" v-model.lazy="username">
</div>
<script src="js/vue.js"></script>
<script>
const vm=new Vue({
el:'#app',
data:{
username:'zhangsan',
n1:1,
n2:2,
},
methods:{
showName(){
console.log(`用户名是:"${this.username}"`)
}
}
})
</script>
</body>
</html>
11.条件渲染指令
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<button @click="toggleFlag">移除flag</button>
<p v-if="flag">这是被 v-if 控制的元素</p>
<p v-show="flag">这是被 v-show 控制的元素</p>
</div>
<script src="js/vue.js"></script>
<script>
const vm=new Vue({
el:'#app',
data:{
flag:true,
},
methods:{
toggleFlag(e){
this.flag=false
}
}
})
</script>
</body>
</html>
12.v-if配套的指令
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<div v-if="type==='A'">优秀</div>
<div v-else-if="type==='B'">良好</div>
<div v-else-if="type==='C'">一般</div>
<div v-else>差</div>
</div>
<script src="js/vue.js"></script>
<script>
const vm=new Vue({
el:'#app',
data:{
type:'A'
},
})
</script>
</body>
</html>
13.v-for的基本用法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<div id="app">
<ul>
<li v-for="(item,index) in list" :key="item.id" style="list-style: none">索引是:{{index}} id是:{{item.id}} 姓名是:{{item.name}}</li>
</ul>
<table class="table table-bordered table-hover table-striped" >
<thead>
<th>索引</th>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>电话</th>
</thead>
<tbody>
<tr v-for="(item,index) in list" :key="item.id" :title="item.name+index">
<td>{{index}}</td>
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td>{{item.age}}</td>
<td>{{item.tel}}</td>
</tr>
</tbody>
</table>
</div>
<script src="js/vue.js"></script>
<script>
const vm=new Vue({
el:'#app',
data:{
list:[
{id:1,name:'张三',age:20,tel:15938882555},
{id:2,name:'李四',age:20,tel:15938882555},
{id:3,name:'王五',age:20,tel:15938882555},
{id:4,name:'孙六',age:20,tel:15938882555},
]
}
})
</script>
</body>
</html>
14.强调v-for循环中key值的注意点
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<input type="text" v-model="name">
<button @click="addNewUser">添加</button>
<ul>
<li v-for="(user,index) in users" :key="users.id">
<input type="checkbox"/>
姓名:{{user.name}}
</li>
</ul>
</div>
<script src="js/vue.js"></script>
<script>
const vm =new Vue({
el:'#app',
data:{
users:[
{id:1,name:'张三'},
{id:2,name:'李四'},
{id:3,name:'王五'},
]
},
methods:{
addNewUser(){
this.users.unshift({id:this.nextId,name:this.name})
this.name=''
}
}
})
</script>
</body>
</html>
15.label的for属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<input type="checkbox" id="cb1">
<label for="cb1">男</label>
<hr>
<input type="checkbox" id="cb2">
<label for="cb2">女</label>
</div>
<script src="js/vue.js"></script>
<script>
const vm =new Vue({
el:'#app',
data:{}
})
</script>
</body>
</html>
|