简体   繁体   中英

update follow status after axios request in Vue

i need to update the follow an unfollow button after axios request

    <template>
        <div v-if="isnot">
        <a href="#"  @click.prevent="unfellow" v-if="isfollowing" >unFellow</a>
        <a href="#" @click.prevent="fellow"  v-else >Fellow</a>
        </div>
    </template>

My Methods

            fellow () {
                axios.post(`/@${this.follower}/follow/`)
            },
            unfellow () {
                axios.post(`/@${this.follower}/unfollow/`)
            },
        }

A basic example:

fellow () {
     var self = this;
     axios.post(`/@${this.follower}/follow/`)
     .then(function (response) {
          self.isfollowing = true;
     })
     .catch(function (error) {
          console.log( error.response.data);
     });
},

Axios has a series of methods you can execute after the response arrives. In the case of a post call your structure can be something like this

axios.post(YOUR ROUTE)
  .then(function (response) {
    //executes after getting a successful response
    // here you can change your 'isfollowing' variable accordingly

  })
  .catch(function (error) {
    //executes after getting an error response
  });

Fast way:

<template>
    <div v-if="isnot">
        <a href="#"  @click.prevent="fellowUnfellow" v-if="isfollowing" >{{isfollowing ? "unFellow" : "Fellow"}}</a>
    </div>
</template>

fellowUnfellow () {
   axios.post(`/@${this.follower}/follow/`).then(function (r) {
       this.isfollowing = !this.isfollowing;
   })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM