vue在组件中删除实例中数据splice


<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app">
    <todo>
        <todo-title slot="todo-title" v-bind:divmessage="message"></todo-title>
        <todo-items slot="todo-items" v-for="(list,index) in lists"  v-bind:str="list" v-bind:index="index" ></todo-items>
    </todo>
</div>

<!--1.导入Vue.js-->
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js"></script>
<script>
    Vue.component("todo",{
        template: "<div>\
                <slot name='todo-title'></slot>\
                <ul>\
                    <slot name='todo-items'></slot>\
                </ul>\
            </div>"
    })
    Vue.component("todo-title",{
        props: ["divmessage"],
        template: "<div>{{divmessage}}</div>"
    })
    Vue.component("todo-items",{
        props: ["str","index"],
        template: "<li>{{str}}--{{index}}<button  v-on:click='remove1(index)'>删除</button></li>",
        methods: {
            remove1: function (index) {
                vm.lists.splice(index,1)
            }
        }
    })
    var vm=new Vue({
        el: "#app",
        data: {
            message: "课程",
            lists: ["java","web","net"]
        },
        /*methods: {
            removeItems: function (canshu) {
                this.lists.splice(canshu,1)
                alert(this.lists)
            }
        }*/
    });
</script>

</body>
</html>

删除列表的一条数据,在Vue Element可以使用以下步骤: 1. 首先,需要在Vue实例定义一个数组,用来存储列表数据。可以给这个数组取一个名字,比如list。在定义数组时,可以在Vue实例的data属性进行定义,例如: ```javascript data: { list: [ { name: '张三', age: 18 }, { name: '李四', age: 20 }, { name: '王五', age: 22 } ] } ``` 2. 然后,在页面使用Vue Element的表格组件(el-table)来展示这个数组数据,例如: ```html <el-table :data="list"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column label="操作"> <template slot-scope="scope"> <el-button @click="handleDelete(scope.$index)">删除</el-button> </template> </el-table-column> </el-table> ``` 在表格的列定义,需要将“操作”列使用插槽(slot)进行自定义。在插槽模板,定义一个按钮,并绑定一个点击事件(handleDelete),用来删除列表的一条数据。其,scope.$index是Vue Element提供的一个属性,表示当前行的索引。通过这个索引,可以知道需要删除的是哪一条数据。 3. 最后,在Vue实例定义一个方法(handleDelete),用来从数组删除数据。例如: ```javascript methods: { handleDelete(index) { this.list.splice(index, 1); } } ``` 这个方法接收一个参数(index),表示需要删除数据在数组的索引。然后,使用数组的splice方法,将这个数据从数组删除。 通过以上步骤,就可以在Vue Element删除列表的一条数据了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Archie_java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值