Modify follow status once axios request is completed in Vue

I have a requirement to update the follow and unfollow button following an 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>

Here are my Methods:

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

Answer №1

Here is a simple illustration:

associate() {
     let me = this;
     axios.post(`/@${this.friend}/connect/`)
     .then(function (response) {
          me.isconnected = true;
     })
     .catch(function (error) {
          console.log(error.response.data);
     });
},

Answer №2

When utilizing Axios, there is a variety of methods that can be implemented once the response is received. For instance, in the scenario of making a post call, your code structure may resemble the following:

axios.post(YOUR ROUTE)
  .then(function (response) {
    // This block runs after receiving a successful response
    // Here, you have the opportunity to adjust your 'isfollowing' variable as needed

  })
  .catch(function (error) {
    // This block executes upon encountering an error response
  });

Answer №3

Quick method:

<template>
    <div v-if="isnot">
        <a href="#"  @click.prevent="toggleFriendship" v-if="isfollowing" >{{isfollowing ? "Unfriend" : "Friend"}}</a>
    </div>
</template>

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering a 502 Bad Gateway error when trying to deploy Nuxt.js to Google App Engine

Has anyone attempted to deploy a Nuxt app on Google App Engine? I have tried deploying from both the regular Nuxt and Express templates, but I keep encountering a 502 Bad Gateway error. I haven't made any modifications to the create-nuxt-app command. ...

Ways to hide notifications by setting a timer while keeping the delete option visible

Presently, this is the code I am working with using Javascript and Vue.js. I have an array called Messages.length that contains messages. When the x button is clicked, it triggers the "clearMessages(item)" function on the server side. However, I also aim ...

Skip using bootstrap-vue icons by utilizing the Webpack IgnorePlugin

After analyzing with Webpack bundle analyzer, I discovered that the icons from the bootstrap-vue package are a whopping 535kb in size. Knowing this, I've decided not to utilize them in my project. I attempted to exclude the entire package using a web ...

What is the best way to access the attribute of an AJAX response?

resp.getWriter().write("msg=1?id=" + l1); Within the code snippet below, I am able to retrieve the responseText. However, I am wondering how can I extract an attribute from the response text. Sample AJAX Code: function updatecategories(cu) { var r1 = ...

How can I retrieve the name of the upcoming middleware function in Express.JS?

Hey there, I'm currently facing a challenge with retrieving the names of middleware functions in a specific request route. Let's consider the code snippet below as an example: const authorizeRoute = (req,res,next) => { let nextFunctionName = ...

Request the generic password prior to revealing the concealed div

I want to implement a feature where a hidden div is shown once a user enters a password. The password doesn't need to be stored in a database, it can be something simple like 'testpass' for now. Currently, I have written some code using Java ...

html.input javascript/jquery

This is an example of an AJAX form with 2 textboxes and a radio button to choose between unlocking and resetting the password. The goal is to make the password label and textbox disappear when the "Unlock" option is selected. However, this cannot be achi ...

Updating a string in JavaScript by dynamically adding values from a JSON object

In my current function, I am making a request to fetch data and storing it as an object (OBJ). Afterwards, I make another request to get a new URL that requires me to update the URL with values from the stored data. The information saved in the object is ...

jQuery's AJAX functionality may not always register a successful response

Below is the code snippet I am currently working with: $(".likeBack").on("click", function(){ var user = $(this).attr("user"); var theLikeBack = $(this).closest(".name-area").find(".theLikeBack"); $.a ...

What are some strategies for preventing unused setState functions in React? Is it possible to create a React useState without a setter function

Currently working on reducing or eliminating npm warnings related to the React site. Many of these warnings are due to the setState function, which is marked as 'unused' in the code snippet below. const [state, setState] = useState('some st ...

The text field is being styled with inline styles automatically

I am trying to set a custom width for a textarea in my form, but the inline styles are automatically overriding my CSS. I have checked my scripts, but I am unsure which one is manipulating the DOM. If you have any insight, please let me know. This is the ...

What is the best way to structure a JSON object with keys that can change dynamically?

var data= [{_id: "5a93cbd49ae761a4015f6346", nombre: "Chicago - Missouri", longitud: "-94.6807924", latitud: "38.287606"}, { _id: "5a93ca539ae761a4015f6344", nombre: "Boston - Central Falss", longitud: "-71.4111895", latitud: "41.8902971"}, { _id: "5a93cc ...

Is it possible to access the Firebase user object beyond the confines of the Firebase function?

Despite successfully logging users into my application using Google Auth from Firebase, I am facing an issue where the User object does not display on the front end of my application (which utilizes Pug templates). How can I resolve this? The code snippet ...

When utilizing a JQuery plugin for sliders, Dart does not function in the same way that traditional JavaScript does

Starting out with Dart for Front-End development has been a bit challenging for me. I am trying to incorporate a JQuery plugin called FlexSlider using the Js-Interop Dart Library. However, it's not functioning as expected compared to pure JavaScript, ...

The issue with the jQuery class change not being triggered in Internet Explorer seems to be isolated, as Chrome and

This little jQuery script I have is supposed to show a fixed navigation menu once the page has been scrolled below 200px, and then change the class on each menu list item to "current" when that section reaches the top of the viewport. The issue is that th ...

Tips for displaying a notification after a successful form submission using jQuery and AJAX

While attempting to submit a PHP form using jquery $.ajax();, I have encountered a peculiar issue. The form is successfully submitted, however, when I try to display an alert message - alert(SUCCESS); on success, it does not work as expected. Any ideas on ...

Error in Visual Studio with Angular 2 build: 'Promise' name not found

I recently started exploring Angular2 and followed the instructions provided in this quickstart guide: https://angular.io/guide/quickstart Everything seems to be working well after running npm install, but now I want to work on it within Visual Studio usi ...

Is webpack necessary for segregating dependencies during installation?

Currently, I'm diving into a tutorial on webpack and it's been three days already, but confusion still reigns! I am delving into the commands: npm i webpack --save-dev I find myself puzzled by the '--save-dev' in the above command whi ...

What could be causing the 'Invalid element type' error to occur in my React Native application?

`import { StyleSheet, Text } from 'react-native'; import { Provider } from 'react-redux'; import { store } from './store'; import { HomeScreen } from './screens/HomeScreen'; import { SafeAreaProvider } from 'rea ...

An assortment of the most similar values from a pair of arrays

I am seeking an algorithm optimization for solving a specific problem that may be challenging to explain. My focus is not on speed or performance, but rather on simplicity and readability of the code. I wonder if someone has a more elegant solution than mi ...