In Nuxt.js how to push object to array in data attribute
P粉060112396
P粉060112396 2024-02-26 12:31:40
0
1
393

I want to push an object into an array inside a data attribute in my nuxt project. But I am getting error like

Cannot read property of undefined (read 'push')

This is my js part

<script>
export default {
  data() {
    return {
      form: {
        gallery: [{
          id: null,
          imgurl: null
        }]
      }
    }
  },

  methods: {
    async uploadGallery() {
      var myimg = "imageurl getting after upload";
      var imgData = {};
      imgData['imgurl'] = myimg;
      imgData['id'] = "someid";
      this.form.gallery.push(imgData);
    },
  }
}
</script>

P粉060112396
P粉060112396

reply all(1)
P粉593118425

I tried your code like above. When I click the button it is working. You may not be able to access form.gallery because the uploadGallery triggered is in the wrong lifecycle hook.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template