Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

boyjarv's avatar

Can I define axios as a functional component in Vue 3 - similar to Vue2 mixins

I am using axios all over the place,how can I make this into a functional component so I can change it on the fly? Thanks

0 likes
12 replies
martinbean's avatar

@boyjarv Your question makes no sense. Axios is a library. What do you mean, make it into a functional component? And what do you mean by “change it on the fly”?

Axios is a library. That makes network requests. What is there to “change on the fly”?

boyjarv's avatar

@martinbean ok I think what I mean is, can I put my API calls into a Functional Component? And how?

martinbean's avatar

@boyjarv No. Why are you trying to? Functional components have no state or logic. And a component that’s doing network calls clearly does have state and logic.

martinbean's avatar

@boyjarv Why? What exactly are you trying to make “composable”? Axios is a library. You use it to accomplish tasks (specific network requests). It isn’t something that really lends itself to being “composable” because you already use it how you need to when creating and executing your network calls.

MohamedTammam's avatar

@martinbean Mostly, I agree. I haven't made into composable before.

However, sometimes it has a really good benefit.

Take this as an example

const { data, loading, exec, error, status } = useAxios();

It's much cleaner and that logic can be used everywhere.

MohamedTammam's avatar

@martinbean Full component example

<template>
  <div>
    <p>current Id {{ id }}</p>
    <p>
      <button @click="id--">prev</button>
      <button @click="id++">next</button>
    </p>
    <p v-if="loading">loading...</p>
    <div v-else>
      <p>Status: {{ status }}</p>
      {{ data }}
    </div>
  </div>
</template>

<script>
import { ref, watch } from "@vue/composition-api";
import { useAxios } from "@vue-composable/axios";

export default {
  name: "axios-example",
  setup() {
    const id = ref(1);
    const { data, loading, exec, error, status } = useAxios();

    watch(id, id => {
      exec({
        method: "GET",
        url: "https://reqres.in/api/user/" + id
      });
    });

    return {
      id,
      data,
      loading,
      status
    };
  }
};
</script>
martinbean's avatar

@MohamedTammam It still doesn’t really make sense to be honest.

What is the advantage of using exec({}) over just making an Axios call? You’re still passing a URL, and any request data, to that exec call.

MohamedTammam's avatar

@martinbean To make it reusable. Now you have loading, error and status variable which require a boilerplate code that keep repeating.

psrz's avatar

@boyjarv

Maybe you are thinking of something like this ? https://robsontenorio.github.io/vue-api-query/

You define some models on your vue application and those will use axios under the hood. You do need to build the api in a certain in order to work but it can help tucking away all those calls.

Please or to participate in this conversation.