vue(Js)從陣列中刪除元素
阿新 • • 發佈:2018-12-09
使用方法:arr.splice(arr.indexOf(ele),length):表示先獲取這個陣列中這個元素的下標,然後從這個下標開始計算,刪除長度為length的元素
這種刪除方式適用於任何js陣列
eg:
<template> <div class="users"> <button type="button" class="btn btn-danger" v-on:click="deleteUser(user)"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span>刪除</button> </div> </template> <script> //引入jquery export default { data(){ return { users:[ { name:'zx', age:18, addrress:'江蘇南京', email:'
[email protected]', contacted:false, }, { name:'zhiyi', age:19, addrress:'中國北京', email:'[email protected]', contacted:false, }, { name:'zhuxu', age:20, addrress:'中國上海', email:'[email protected]', contacted:false, }, ] } }, methods:{ deleteUser:function(user){ //表示先獲取這個元素的下標,然後從這個下標開始計算,刪除長度為1的元素 this.users.splice(this.users.indexOf(user),1); } } }; </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <!--scope只會影響到當前元件的樣式--> <style scoped> </style>