vue指令2
v-for
v-on补充
- 传递自定义参数,事件修饰符(https://cn.vuejs.org/v2/api/#v-on)
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>v-for</title>
</head>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<body>
<div id="app">
<ul>
<li v-for="(item,index) in arr">
{{item}}-{{index}}
</li>
</ul>
<input type="button" value="添加数据" @click="add">
<input type="button" value="移除数据" @click="remove">
<h2 v-for="item in arrname" :title="item.name">
{{item.name}}
</h2>
</div>
<hr>
<div id="app2">
<input type="button" value="点击" @click="doIt(555,'zany')">
<input type="text" @keyup.enter="sayhi">
</div>
<hr>
<div id="app3">
<input type="text" v-model="message" @keyup.enter="getM">
<h2>{{message}}</h2>
<input type="button" @click="setm" value="修改文本框的值为zany">
</div>
</body>
<script>
var app=new Vue({
el:"#app",
data:{
arr:[1,2,3,4,5],
arrname:[
{name:"小明"},
{name:"小红"},
{name:"小刚"}
]
},
methods:{
add:function(){
this.arrname.push({name:"小王"})
},
remove:function(){
this.arrname.shift();
},
},
})
var app2=new Vue({
el:"#app2",
methods:{
doIt:function(p1,p2){
console.log("doit");
console.log(p1);
console.log(p2);
},
sayhi:function(){
alert("吃了嘛");
},
},
})
var app3=new Vue({
el:"#app3",
data:{
message:"啦啦啦"
},
methods:{
getM:function(){
alert(this.message);
},
setm:function(){
this.message="zany"
},
}
})
</script>
</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>记事本</title>
</head>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<body>
<div id="app">
<input type="text" v-model="print" @keyup.enter="add()" placeholder="请输入任务">
<ul>
<li v-for="(item,index) in list">
<div>
<span>{{index+1}}.</span>
<label>{{item}}</label>
<input type="button" value="×" @click="del(index)" >
</div>
</li>
<label v-text="'一共'+list.length+'item left'" v-show="list.length!=0"></label>
<button @click="clear()" v-if="list.length!=0">Clear</button>
</ul>
</div>
<script>
var app=new Vue({
el:"#app",
data:{
list:["吃饭","睡觉","打代码"],
print:""
},
methods:{
add:function(){
this.list.push(this.print);
},
del:function(index){
this.list.splice(index,1);
},
clear:function(){
this.list=[];
}
}
})
</script>
</body>
</html>