Home > Back-end >  Vue.js updating a variable inside methods not working
Vue.js updating a variable inside methods not working

Time:01-01

i'm working on a Vue.js app , this is my data :

data() {
  return {
    formAddNew: {
        cover: "",
        title: "",
        date: "",
        content: "",
        author: "",
        attachement: "",
    },
  }
}

I want to update cover after an axios request this how it looks :

          axios.post('/upload.php',
                formData,
                {
                    headers: {'Content-Type': 'multipart/form-data'}
                }
            ).then(function(data){
                this.formAddNew.cover = data.data;
            })
            .catch(function(){
                console.log('Error!')
            }); 

But it doesn't work i don't know why , please help me fix it .

CodePudding user response:

Try to use (data)=> instead function(data) to get access to this in the callback context :

 ).then((data)=>{
               this.formAddNew.cover = data.data;
            })
  • Related