簡體   English   中英

Vue JS通過唯一ID刪除數組中的對象?

[英]Vue JS delete objects in array by unique id?

我正在使用Vue.js刪除對象數組中的對象,問題是我找不到通過其唯一ID刪除對象的方法。 我正在使用vue.js版本1。我還需要一種更新同一對象的方法(它必須是反應性的,因此我的視圖會自動更新)。

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Vue Js - components</title> </head> <body> <!-- index.html --> <div id="app"> <div class="container-fluid"> <ul class="list-group"> <post v-for="posty in posts" :posty="posty" track-by="uuid"></post> </ul> </div> </div> <template id="my-component"> <div v-if="posty.votes === '15'"> <button v-on:click="testFunc(posty.uuid)">{{posty.title}}</button> </div> <div v-else> <button v-on:click="testFunc(posty.uuid)">No</button> </div> </template> <script src="vue-v1.js"></script> <script> Vue.component('post', { template: "#my-component", props: ['posty'], methods: { testFunc: function(index){ this.$parent.parentMethod(index); } } }); var vm = new Vue({ el: "#app", data: { posts: [{ uuid: '88f86fe9d', title: "hello", votes: '15' }, { uuid: '88f8ff69d', title: "hello", votes: '15' }, { uuid: '88fwf869d', title: "hello", votes: '10' }] }, methods: { parentMethod: function(index){ Vue.delete(this.posts, index); } } }); </script> </body> </html> 

https://jsbin.com/fafohinaje/edit?html,js,控制台,輸出

我不知道您的Vue.delete方法應該在這里代表什么,所以您可以使用數組拼接

methods: {
  parentMethod: function(index){
    this.posts.splice(index, 1)
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM