javascript - Laravel and axios clear Form after submit -


in laravel app i'm submitting form vue , axios. after submit want clear input field in form, wont work.

html:

<form method="post" enctype="multipart/form-data" v-on:submit.prevent="addpost">   <textarea id="post_area" v-model="content"></textarea>   ..... </form> 

js:

const app = new vue({ el: '#app', data: {   content: '',   posts: [] },  ......  .then(function (response) {    if(response.status===200) {       //reload posts       app.posts = response.data;       this.content = '';    } }) 

it wont clear out input field.

this not point vue instance in promise's success callback

use arrow function instead. arrow function binds value of this lexically

const app = new vue({ el: '#app', data: {   content: '',   posts: [] },  ......  .then( (response) => {    if(response.status===200) {       //reload posts       app.posts = response.data;       this.content = '';    } })  

or create local variable pointing correct vue instance , use access data property this:

methods:{     addpost(){         var vm = this;          //.....axios post          .then( (response) => {             if(response.status===200) {                 //reload posts                 app.posts = response.data;                 vm.content = '';             }         })       } } 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -