I want to add on extra property(showMore
) dynamically to array of object inside v-for. is it possible to do it inside v-for
like:
this.students = [
{
name: 'anonymous',
class: 'Sixth',
otherInfo: "..."
},
{
name: 'anonymous2',
class: 'Sixth',
otherInfo: "..."
}
]
<div v-for="student in students">
<div>{{student.name}}</div>
<div>{{student.class}}</div>
<div @click="student.showMore = true">Show more +</div>
<!-- it was possible in angularjs. is it possible in vuejs ?-->
</div>
here i am adding one more property (showMore
) to array of object(student
) by clicking on Show more. is it possible inside v-for? if not then how to achieve this(with best practice)?
Use an index signature to dynamically add properties to an object, e.g. const obj: {[key: string]: any} = {} . Index signatures are used when we don't know all of the names of a type's properties and the type of their values ahead of time.
Add Element to Array With push() Method To add an element to an array in Vue, call the push() method in the array with the element as an argument. The push() method will add the element to the end of the array. Clicking the button adds a new fruit element.
Simply use v-bind like :ref="'element' + result.id" or ref="`element${result.id}`" .
Sure, but it will not be reactive though. If you want that, you need to use:
vm.$set( target, key, value )
https://vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With