P
P
postya2021-01-04 18:33:33
Vue.js
postya, 2021-01-04 18:33:33

How to use axios in Vue 3 with Typescript?

I need to make a request to the server with axios, get all posts and output all posts

How can I use axios in a Vue 3 project with Typescript?
well, or at least without typescript

Please provide an example with a get and post request

What I tried:

Created a file for the axios service in the directory service
SnippetsService.ts

import axios from "axios";

export default class SnippetsService {
  public static getAllSnippets() {
    return axios.get("http://localhost:13050/snippets");
  }
}


In the component, I used a method from the class like this:
setup() {

let allSnippets = ref({});

created(() => {
      allSnippets = SnippetsService.getAllSnippets()
        .then(response => {
          console.log(response);
          allSnippets = response;
        })
        .catch(error => {
          console.log(error);
        });
    });
    return { allSnippets };
}


Got an error:
5ff335fa30c6b247279065.jpeg

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Igor Makhov, 2021-01-04
@Igorgro

In the browser it is better to use fetch and not axios

Y
Yahya, 2017-03-20
@JarvisVRTX

When clicking, check for the existence of the .hasClass() class

N
Nikolay, 2017-03-20
@nickolyashka

You can do it on tabs, or open by id and use toggle.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question