一 、vue 支持觸發(fā)事件:
寫法是:
v-on:click="fn" 等價于 @click="fn" 等價于
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>事件</title>
</head>
<body>
<div id="app">
<div v-on:click="fn">
點我啊, 小哥哥!
</div>
</div>
<script src="./node_modules/vue/dist/vue.js"></script>
<script>
let vm = new Vue({
el:"#app",
data:{},
methods:{
fn(){alert(1)}
}
})
</script>
</body>
</html>
二嗤攻、TODO:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>TODO</title>
</head>
<body>
<div id="app">
<input type="text" v-model="val" @keyup="add" />
<ul>
<li v-for="(a,index) of arr">
{{a}}<button @click="remove(index)">刪除</button>
</li>
</ul>
</div>
<script src="./node_modules/vue/dist/vue.js"></script>
<script>
let vm = new Vue({
el: "#app",
methods: {
add(e) {
if (e.keyCode === 13) {
console.log(this.val);
this.arr.push(this.val);
this.val = '';
}
},
remove(i){
this.arr=this.arr.filter((item,index)=>index!==i)
}
},
data: {
val: '',
arr: [],
}
})
</script>
</body>
</html>