自定义事件
指令 | 作用 |
---|
v-bind | 绑定前端界面和vue的数据 ,缩写:冒号 |
props | 绑定前端与组件的数据 |
v-on | 事件绑定 缩写:@ |
v-once | 渲染一次 |
v-pre | 跳出渲染 |
v-model | 双向绑定,用于文本框、单选、复选、下拉 |
v-cloak | 消除闪烁 |
this.$emit | 将事件从组件分发回前端界面 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<phone>
<phone-title slot="phone-title" :title="title"></phone-title>
<phone-items slot="phone-items" v-for="(item,index) in phoneItems"
:item="item" v-bind:index="index" @remove="removeItems(index)" :key="index"></phone-items>
</phone>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
Vue.component("phone",{
template:
'<div>\
<slot name="phone-title"></slot>\
<ul>\
<slot name="phone-items"></slot>\
</ul>\
</div>'
});
Vue.component("phone-title",{
props: ['title'],
template:'<div>{{title}}</div>'
});
Vue.component("phone-items",{
props: ['item','index'],
template: '<li>{{item}} <button @click="remove">删除</button></li>',
methods: {
remove: function (index){
this.$emit('remove',index);
}
}
});
var vm = new Vue({
el:"#app",
data: {
title: "手机列表",
phoneItems: ["诺基亚","华为","OPPO","VIVO"]
},
methods: {
removeItems: function (index){
console.log("已删除" + this.phoneItems[index]);
this.phoneItems.splice(index,1);
}
}
});
</script>
</body>
</html>