0

i am making vue application i want to add more data which is fetching through laravel return in array without loosing old data

my methods is

    getData () {
        axios.get('get/users')
            .then(response => {
                this.queue = response.data.users
            })
        .catch(e => {
          this.errors.push(e)
        })
    }

and my data

    data: function () {
        return {
            queue: []
        }
    },

Full Script is

  data: function () {
        return {
            queue: []
        }
    },

  created () {
    this.getData()
  },
  methods: {


    getData () {
        axios.get('get/users')
            .then(response => {
                this.queue = response.data.users
            })
        .catch(e => {
          this.errors.push(e)
        })
    },

    decide (choice) {
      this.$refs.tinder.decide(choice)
    },

    submit (choice) {
      switch (choice) {
        case 'nope': // 左滑
          break;
        case 'like': // 右滑
          break;
        case 'super': // 上滑
          break;
      }
      if (this.queue.length < 2) {
        this.getData()
      }
    }
  }

here is video link

https://youtu.be/iv82EGMD4XA

i want when queue have 2 data left trigger this.getData() fetch users and add array data to queue without loosing old queue data

1 Answer 1

1

You can use the concat function

this.queue = this.queue.concat(response.data.users); 

In the axios response

Instead of the

this.queue = response.data.users
Sign up to request clarification or add additional context in comments.

1 Comment

Would the reactivity kick in in this case? The array ref never changes and elements are added to the array so it may not be detected as a change. I think it would make better sense to use it like this.queue = this.queue.concat(response.data.users); concat is listed as a non-mutating method in the docs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.