简体   繁体   English

推入数组中的另一个对象

[英]push another object in array

This is my code, it's not the best one, but its what I know at the moment. 这是我的代码,它不是最好的代码,但是它是我目前所知道的。 It's strange why everytime I push a new object into the array it doesn't enter in my layout. 奇怪的是为什么每次我将一个新对象推入数组时,它都不会进入我的布局。 I haven't tried a solution with css, I think the problem is with the javascript 我尚未尝试使用CSS解决方案,我认为问题出在javascript

 export default { data:()=>{ return{ cards:[ {title: 'Azorius de volta ao topo!', subtitle: 'Baralho volta a vencer um Magic Fest Modern após longo hiato.', head: 'Modern'}, {title: 'Pauper no Papel', subtitle: 'Wizards oficializa o formato', head: 'Pauper'}, {title: 'Hogaak Winter', subtitle: 'Possível última semana do monstro', head: 'Modern'} ], } }, components:{ Modal }, methods:{ loadMore(){ console.log(this.cards); this.cards.push({title: 'alfafa', head: 'alfafa'}) } } } 
 .footer-card{ background-image: linear-gradient(to bottom, #1a1a1a, #666666); } .icons{ color: #fff !important; } .title{ font-family: 'Arial'; font-size: 12px } .subtitle{ font-family: 'Arial'; font-size: 12px; font-weight: 100; } .card-zise{ width: 21.500em !important; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <v-flex xs6 class="mx-auto" style="display:flex"> <v-card class="mx-auto" style="padding: 0 0.625em" v-for="card in cards" :key="card" > <v-list-item> <v-list-item-avatar color="grey"></v-list-item-avatar> <v-list-item-content> <v-list-item-title class="headline">{{card.head}}</v-list-item-title> </v-list-item-content> </v-list-item> <v-img src="https://cdn.vuetifyjs.com/images/cards/mountain.jpg" height="194" ></v-img> <v-card-text v-bind="cards" class="title"> {{ card.title }} <br/> </v-card-text> </v-card> </v-flex> <v-btn @click="loadMore()">Carregar Mais</v-btn> 

here is the results of the push 这是推的结果

You shouldn't be using an object as key attribute. 您不应该将对象用作key属性。 Try it like this: 像这样尝试:

<v-card
    class="mx-auto"
    v-for="(card, cardIndex) in cards" :key="'card-' + cardIndex"
  >
...
</v-card>

Firstly like this you don't have objects as key and also not just a number . 首先,像这样,您没有对象作为key ,也没有number

Pretty sure your problem is how you deal with uniqueness of Array items. 可以肯定,您的问题是如何处理数组项的唯一性。 You are using the actual card object as the key . 您正在使用实际的卡对象作为key Don't do that. 不要那样做 Try to pick a unique property of the element of the Object within the Array. 尝试选择数组中Object元素的唯一属性。

If there is no unique piece to it, you could potentially use the index of the Array: 如果没有唯一的块,则可以使用Array的索引:

<div v-for="(card, idx) in cards" :key="idx">{{card}}</div>

or just leave the key off: 或只留下key

<div v-for="card in cards">{{card}}</div>

Additionally, if your issue is that you want to change the order of the cards, you need to adjust how you are inserting into the cards array. 此外,如果您的问题是要更改卡的顺序,则需要调整插入cards阵列的方式。 Vue will iterate whatever you give it in the order that you provide it. Vue将按照您提供的顺序迭代您提供的任何内容。 So if you want the new card to be at the top, you need to insert it in the first position. 因此,如果您希望新卡位于顶部,则需要将其插入第一个位置。 One way to do that is to use ... spread syntax: 一种方法是使用...传播语法:

this.cards = [{foo: "bar"}, ...this.cards];

Where {foo: "bar"} is the new Object to insert and ...this.cards is the expansion of the Array this.cards . 其中{foo: "bar"}是要插入的新对象,而...this.cards是Array this.cards的扩展。

 const app = new Vue({ el: "#app", data: () => { return { cards: [{ title: 'Azorius de volta ao topo!', subtitle: 'Baralho volta a vencer um Magic Fest Modern após longo hiato.', head: 'Modern' }, { title: 'Pauper no Papel', subtitle: 'Wizards oficializa o formato', head: 'Pauper' }, { title: 'Hogaak Winter', subtitle: 'Possível última semana do monstro', head: 'Modern' } ], } }, methods: { loadMore() { this.cards = [{ title: 'alfafa', head: 'alfafa' }, ...this.cards ] } } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <div> <div v-for="card in cards"> <div>{{card.head}}</div> </div> </div> <button @click="loadMore()">Carregar Mais</button> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM