Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use axios in Vue (Typescript)?

I would like to use axios in vue (Typescript) but I get into trouble with my code. This is my main.ts

import axios from 'axios'
Vue.prototype.$axios = axios
axios.defaults.baseURL = 'http://192.168.1.225:8088'

and this is my vue code screenshot here

This is the first time I use typescript,before I used it another way in javaScript and I did not have any problem, so how can I use it in TypeScript?

Thank you for your time and solution.

like image 792
william Avatar asked Aug 28 '18 08:08

william


People also ask

Can we use Axios in TypeScript?

Create an Axios Config File in TypeScriptAxios provides many useful types and can be used to create a config file which we can further use to make REST API calls throughout the application. Copy import Books from './api' // config added in api. ts file const [books, setBooks] = React.

What is the difference between Axios and Vue-Axios?

vue-axios is just a wrapper, exposing axios to components as this. axios , this. $http , or Vue.

How to make REST API calls in typescript with Axios?

With the growing demand for TypeScript, types have been added to the Axios library. This tutorial will use Axios to make REST API calls in TypeScript. The first step is to install Axios in a project. Axios can be installed in a NodeJs or React project. Now, Axios can be used in the project with other packages.

What is Axios in Vue JS?

Axios is a promise-based HTTP client library for both browsers and Node.js applications, which means it can be used in both frontend JavaScript applications and backend Node servers. In this article, we will look at how to use Axios in a simple Vue.js application.

How to add vue router to VUE 3 typescript?

Add Vue Router to Vue 3 Typescript example – Run the command: npm install vue-router@4. – In src folder, create router.ts and define Router as following code:

What are the components of Vue tutorialslist?

– There are 3 components: TutorialsList, TutorialDetails, AddTutorial. – router.ts defines routes for each component. – http-common.ts initializes axios with HTTP base Url and headers. – TutorialDataService has methods for sending HTTP requests to the Apis. – vue.config.js configures port for this Vue Client.


1 Answers

I'm encapsulate HTTP/REST operations in separate .ts files. Here I also use async/await to have better readable code. Each method declared its input and return types.

import axios from 'axios'

const http = axios.create({
  baseURL: `${SOME_SERVICE_URL}/base-path`,
  headers: { 'Content-Type': 'application/json' }
})

export async function getItemById (itemId: string): Promise<Item> {
  const response = await http.get(`/${itemId}`)
  return response.data
}

export async function createItem (item: Item): Promise<Item> {
  const response = await http.post('/', JSON.stringify(item))
  return response.data
}

like image 164
Andreas Bauer Avatar answered Sep 20 '22 07:09

Andreas Bauer