《Vue.js实战》一书p183 练习1 & 2 试做

练习1 : 给每条留言都增加一个删除的功能。
练习2 :将该示例的re nd er 写法改写为te mp lat e 写法,加以对比,总结出两者的差异性,深
刻理解其使用场景。

demo在线效果浏览

解答:

在 list.vue 的 render 函数中,添加如下 代码,当新增的a元素被点击后,向父组件传递自定义事件 del,并传递 index索引

                    h('a',{
                        attrs:{
                            class:'list-del'
                        },
                        on:{
                            click(){
                                _this.handleDel(index);
                            }
                        }
                    },'删除')
        handleDel(index){
            this.$emit('del',index);
        }

app.vue文件做如下修改:A 在list组件上监听自定义事件 del,当事件被触发,调用handleDel,从数组中删除对应留言。

    <list :list="list" @reply="handleReply" @del="handleDel"></list>
    handleDel(index){
      this.list.splice(index,1);
    }
原文地址:https://www.cnblogs.com/sx00xs/p/11380675.html