Vue Js Fetch data from API | Load Data without reoloading Page
Vue js Fetch Data from API using Axios- Axios is a popular HTTP client library that simplifies sending and handling HTTP requests in JavaScript applications.
In Vue.js, we can use Axios to fetch data from an API endpoint by making an asynchronous HTTP GET request using the Axios.get() method. The response data is stored in a variable using Vue's reactive data property.
The fetched data can then be rendered in the Vue template using the v-for directive or other data binding techniques. This code can be used to make API requests and update the Vue component's state with the fetched data.
Thanks for your feedback!
Your contributions will help us to improve service.
How can Vue js be used to fetch data from an API?
The script creates a new Vue application using the Vue.createApp()
method, which takes an object as its argument. Inside this object, the data()
method returns an object containing three properties: lists
, loading
, and errored
.
The mounted()
method is called when the Vue instance is mounted to the DOM, and it uses Axios, a popular HTTP client library, to send a GET request to the CoinDesk API to fetch the current Bitcoin price in various currencies.
If the request is successful, the then()
method sets the lists
property to the Bitcoin price data returned by the API, which is located in the resp.data.bpi
object.
If there is an error, the catch()
method logs the error to the console and sets the errored
property to true
.
Finally, the finally()
method sets the loading
property to false
to indicate that the data has finished loading.
This code is intended to fetch data from an API and display it on the web page. The data()
method sets the initial values of lists
, loading
, and errored
, and the mounted()
method uses Axios to fetch the Bitcoin price data from the CoinDesk API. If the data is successfully fetched, it is stored in the lists
property and can be accessed in the template. If there is an error, the error is logged to the console and the errored
property is set to true
. Finally, the loading
property is set to false
to indicate that the data has finished loading.
Output of Vue Js Fetch data from API
How can I load data in Vuejs without reloading the page?
This Vuejs code demonstrates how to load data without reloading the page. The app fetches currency data from the Coindesk API and displays it in a table. The data is initially set to "Loading..." until the API response is received. After loading, the table with currency information is displayed. The data is updated automatically every 6 seconds using setInterval
. The app also shows a toast message when the data is successfully reloaded. Axios library is used to make the API request.