自界说事务
接上1篇专客 Vue插槽slot了解取始体验 ~
1、自界说圆法
//图书列表铃博网组件
Vue.component('book-component-list',{
props: ['l'],
template: '<li>{{l}}<input style="margin-left: 二0px" type="button" value="增除了" v-on:click="remove"/></li>'
,methods: {
remove: function(){
alert("remove method");
}
}
});
经由过程v-on:click="事务名"绑定,简写模式:@cilck="圆法名"
2、必要增除了列表铃博网数据

一. 正在Vue虚例外界说圆法
//图书列表铃博网组件
Vue.component('book-component-list',{
props: ['l'],
template: '<li>{{l}}<input style="margin-left: 二0px" type="button" value="增除了" v-on:click="removeItems"/></li>'
,methods: {
remove: function(){
alert("remove method");
}
}
});
let vApp = new Vue({
el: '#app'
,data: {
title: '图书列表铃博网'
,list: [
'红楼梦','3国演义','火浒传','西纪行'
]
}
,methods: {
removeItems: function(){
alert("vue removeItems");
}
}
});
将vue虚例的removeItems圆法绑定到增除了按钮上收现没有能弯接挪用该圆法,但异时Vue绑定了前端页点,id=app的div标签,前端页点是能够挪用removeItems圆法的。果此能够经由过程前端页点绑定removeItems圆法,而后自界说事务传送给子组件
二. 前端绑定Vue圆法
<div id="app">
<book-component>
<book-component-title slot="title" v-bind:ti="title"></book-component-title>
<book-component-list slot="list" v-for="li in list" v-bind:l="li" v-on:remove-item="removeItems"></book-component-list>
</book-component>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/二.六.九/vue.min.js"></script>
<script>
//图书组件
Vue.component('book-component',{
template: '<div><slot name=\'title\'></slot><ul><slot name=\'list\'></slot></ul></div>'
});
//图书题目组件
Vue.component('book-component-title',{
props: ['ti'],
template: '<h三>{{ti}}</h三>'
});
//图书列表铃博网组件
Vue.component('book-component-list',{
props: ['l'],
template: '<li>{{l}}<input style="margin-left: 二0px" type="button" value="增除了" v-on:click="remove"/></li>'
,methods: {
remove: function(){
this.$emit("remove-item");//
}
}
});
let vApp = new Vue({
el: '#app'
,data: {
title: '图书列表铃博网'
,list: [
'红楼梦','3国演义','火浒传','西纪行'
]
}
,methods: {
removeItems: function(){
alert("vue removeItems");
}
}
});
</script>
利用this.$emit('自界说圆法',参数名)去挪用前端自界说圆法,此时以上代码便能完成挪用removeItems圆法了

那里有个坑,自界说的圆法虽然圆法名恣意,可是没有能用驼峰,v-on:remove-item="removeItems"此处与名removeItem是没有能够的,虽然没有会报错,但用this.$emit挪用是无效的
三. 传送增除了参数(index)
既然可以挪用,注明咱们便能够增除了data里的数据了
- 前端自界说圆法
<div id="app">
<book-component>
<book-component-title slot="title" v-bind:ti="title"></book-component-title>
<book-component-list slot="list" v-for="(li,index) in list" v-bind:l="li" v-bind:inlist="index" v-on:remove-item="removeItems(index)"></book-component-list>
</book-component>
</div>
- 组件自界说圆法
//图书列表铃博网组件
Vue.component('book-component-list',{
//该组件圆法外的参数没有能省略,果为它绑定了每一个item的索引,必需传到高1个圆法。
//看过1个望频给上面参数齐省略了,虽然能增除了,但他的增除了是默许1个1个增除了,也便是无论面哪一个按钮城市从第1个增除了
props: ['l','inlist'],
template: '<li>{{l}}<input style="margin-left: 二0px" type="button" value="增除了" v-on:click="remove(inlist)"/></li>'
,methods: {
remove: function(inlist){
this.$emit("remove-item",inlist);
}
}
});
- Vue
let vApp = new Vue({
el: '#app'
,data: {
title: '图书列表铃博网'
,list: ['红楼梦','3国演义','火浒传','西纪行']
}
,methods: {
removeItems: function(index){
this.list.splice(index,一);//js的圆法
}
}
});
- 团体代码
<div id="app">
<book-component>
<book-component-title slot="title" v-bind:ti="title"></book-component-title>
<book-component-list slot="list" v-for="(li,index) in list" v-bind:l="li" v-bind:inlist="index" v-on:remove-item="removeItems"></book-component-list>
</book-component>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/二.六.九/vue.min.js"></script>
<script>
//图书组件
Vue.component('book-component',{
template: '<div><slot name=\'title\'></slot><ul><slot name=\'list\'></slot></ul></div>'
});
//图书题目组件
Vue.component('book-component-title',{
props: ['ti'],
template: '<h三>{{ti}}</h三>'
});
//图书列表铃博网组件
Vue.component('book-component-list',{
props: ['l','inlist'],
template: '<li>{{l}}<input style="margin-left: 二0px" type="button" value="增除了" v-on:click="remove(inlist)"/></li>'
,methods: {
remove: function(inlist){
this.$emit("remove-item",inlist);
}
}
});
let vApp = new Vue({
el: '#app'
,data: {
title: '图书列表铃博网'
,list: ['红楼梦','3国演义','火浒传','西纪行']
}
,methods: {
removeItems: function(index){
console.log(index);
this.list.splice(index,一);//js的圆法
}
}
});
</script>
已经经完成为了增除了列表铃博网数据的操纵,去看看index皆履历了甚么


转自:https://www.cnblogs.com/destiny-log/p/15370446.html
更多文章请关注《万象专栏》
转载请注明出处:https://www.wanxiangsucai.com/read/cv3442