Vue Request

Posted on  by admin

Below is a quick set of examples to show how to send HTTP GET requests from Vue to a backend API using fetch() which comes bundled with all modern browsers.

Other HTTP examples available:. Vue + Fetch: POST. Vue + Axios: GET, POST. React + Fetch: GET, POST, PUT, DELETE. React + Axios: GET, POST, PUT, DELETE. Angular: GET, POST, PUT, DELETE. Blazor WebAssembly:GET, POST.

Axios: GET, POST, PUT, DELETE. Fetch: GET, POST, PUT, DELETE. This sends an HTTP GET request from Vue to the npm api to search for all vue packages using the query q=vue, then assigns the total returned in the response to the component data property totalVuePackages so it can be displayed in the component template.

Example Vue component at https://codesandbox.io/s/vue-fetch-http-get-request-examples-rzuqn?file=/app/GetRequest.vue. This sends the same GET request from Vue using fetch, but this version uses an async function and the await javascript expression to wait for the promises to return (instead of using the promise then() method as above).

Example Vue component at https://codesandbox.io/s/vue-fetch-http-get-request-examples-rzuqn?file=/app/GetRequestAsyncAwait.vue. This sends a GET request from Vue to an invalid url on the npm api then assigns the error to the errorMessage component data property and logs the error to the console.

  • The fetch() function will automatically throw an error for network errors but not for HTTP errors such as 4xx or 5xx responses.
  • For HTTP errors we can check the response.ok property to see if the request failed and reject the promise ourselves by calling return Promise.reject(error);.
  • This approach means that both types of failed requests - network errors and http errors - can be handled by a single catch() block.
  • Example Vue component at https://codesandbox.io/s/vue-fetch-http-get-request-examples-rzuqn?file=/app/GetRequestErrorHandling.vue.
  • This sends the same GET request again from Vue using fetch with the HTTP Content-Type header set to application/json.
  • Example Vue component at https://codesandbox.io/s/vue-fetch-http-get-request-examples-rzuqn?file=/app/GetRequestSetHeaders.vue.
  • ⚡️ Vue 3 composition API for data fetching, supports SWR, polling, error retry, cache request, pagination, etc.
  • In the past projects, they were often confused by repeated implementations such as the management of the loading state, the requested throttling and debounce, the caching of request data, and pagination.

Creating Vue project

Whenever we start a new project, we have to manually deal with the above problems, which will be a repetitive work, but also to ensure that the team is consistent.

Usage

VueRequest aims to provide developers with a convenient and fast way to manage the state of the request API.

Thanks

In the development, save repetitive work, and it can be used only with a simple configuration, focusing on the core of the development project.

🚀 All data is reactive. 🔄 Interval polling. 🤖 Automatic error retry. 🗄 Built-in cache. 💧 Throttle and Debounce.

Documentation

⚙️ Powerful pagination extension and load more extensions. 📠 Written in TypeScript.

Install

⚡️ Compatible with Vite. 📦 Out of the box. You can install VueRequest with NPM, YARN, or a