0

I want to create a new item. After created successfully I need to update the item list. When I set the item list from server it fails to update in view.

<v-btn @click="create(item)" >
</v-btn>
data: () => ({
  items: [],
})
methods: {
  create (item) {
    createItem(item).then(response => {
      this.scene = this.scenes.list
      this.item = null
      var options = {
        page: 0,
        itemsPerPage: 10,
      }
      getItems(options).then(function (response) {
        this.items= response.content
      }.bind(this))
    })
},
}

When I debug, 'this.items= response.content' is successfuly set. But in view the data is not updated. How to fix this?

2 Answers 2

1

Since you bind this to another context, this.items is no longer referring to the Vue instance, thus it won't update.

Declare the Vue instance to another variable before createItem, and you should be able to use it:

data: () => ({
  items: [],
})
methods: {
  create (item) {
    const vm = this;
    createItem(item).then(response => {
      this.scene = this.scenes.list
      this.item = null
      var options = {
        page: 0,
        itemsPerPage: 10,
      }
      getItems(options).then(function (response) {
        vm.items= response.content
      }.bind(this))
    })
},
}
Sign up to request clarification or add additional context in comments.

Comments

0

It's a scope(this) issue, Simply you can use arrow function in the response or also you can use async/await approach.

getItems(options).then(response => {
   this.items = response.content
})
async create (item) {
   try {
     const itemResponse = await createItem(item)
     this.scene = this.scenes.list
     this.item = null
     const options = { page: 0, itemsPerPage: 10 }
     const response = await getItems(options)
     this.items = response.content
   } catch() {
      console.log(err)
   }
}

Comments

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.