I have an array and I would like to push some data to it.
this.myArray.push(['a' => 'b']);
Unfortunately this does not work with Vue 3 as myArray
appears to be wrapped in a proxy object.
Does anyone know how to push to an array in Vue 3?
To push items into an array in the data object in Vue. js, we can make a copy of the object to append to the array before we call push with it. to call this.
To push an object into an array, call the push() method, passing it the object as a parameter. For example, arr. push({name: 'Tom'}) pushes the object into the array. The push method adds one or more elements to the end of the array.
With Vue Router, you can use its router. push() function to programmatically navigate between routes on your site. You can either call push() with a string path, or with an object containing either the path or name of the route.
JavaScript Array push() The push() method adds new items to the end of an array. The push() method changes the length of the array. The push() method returns the new length.
It depends how you define the array
ref
:const myArray = ref([1,2,3]);
myArray.value.push(4); // With a `ref` you need to use `value`
reactive
:const myArray = reactive([1,2,3])
myArray.push(4); // push works as normal
Other issues:
Your syntax is incorrect for what's being pushed, maybe you want:
myArray.push({ a: 'b' });
Also, keep in mind there is no component this
access in the Vue 3 composition API.
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