vue-fetch
Simplified fetch API for Vue 2+
7,347
7
| Install | |
|---|---|
npm install vue-fetch |
|
| Latest Version: | 3.0.0 |
| Vue: | 3 |
| Older releases: | 2 |
| License: | ISC |
| Last Updated: | Sep 3, 2026 |
| Links: | GitHub · npm |
Maintainer: bangbang93
vue-fetch
Use fetch in vue just like $http in Angular
polyfill
polyfill you may need
- url-search-params-polyfill
- whatwg-fetch
- es6-promise
Why
Fetch API is awesome... but it has some feature I don't like :()
- if you need cookies, you must pass
credentials: 'include'as second params - pass json while manually
JSON.parse(body)andheaders: new Headers({'content-type': 'application/json' })
So I wrote this small plugin to provide a "better way" ;)
Usage
// optionally use a fetch polyfill
import 'whatwg-fetch'
import VueFetch from 'vue-fetch'
import Vue from 'vue'
Vue.use(VueFetch, {
polyfill: true //should vue-fetch load promise polyfill, set to false to use customer polyfill
});
const vm = new Vue({
});
(async function(){
let login = await vm.$fetch.post('/user/login', {
username: 'somebody',
password: 'wow',
});
if (login.status != 200) {
alert('login error');
}
let user = await vm.$fetch.get('/user/profile');
if (user.status != 200){
alert('can not get profile')
}
vm.user = await user.json();
})()
Inside component
Access via this.$fetch
Outside component
Makes sense to have a separate data layer to keep with Single Responsibility guidelines.
import 'isomorphic-fetch'
import { Fetch } from 'vue-fetch'
// you can pass in mocked fetch or Headers if required
const $ = Fetch({
// fetch,
// Headers,
logging: true
})
export default {
create: async (data) => {
return await $.post('/user', data)
},
updateById: async (id, data) => {
return await $.put(`/user/${id}`, )
},
deleteById: async (id) => {
return await $.delete(`/user/${id}`)
},
findById: async (id) => {
return await $.get(`/user/${id}`)
}
}
Support
GET/DELETE
vm.$fetch.get(url, {
page: 1
});
vm.$fetch.del(url, {
limit: 1
});
POST/PUT/PATCH
vm.$fetch.post(url, {
foo: 'bar'
});
vm.$fetch.put(url, {
foo: 'bar'
});
vm.$fetch.patch(url, {
foo: 'bar'
});
FETCH
vm.$fetch.fetch('POST', url, {
page: 1
}, {
body: 'some info'
});
Related Packages
@meathill/digest-fetch
A tiny fetch wrapper to add digest auth support, without any dependencies.
240
Version History
| Version | Released | Vue | License |
|---|---|---|---|
| 3.0.0 | ^3.2.45 | ISC | |
| 2.0.0 | ^3.2.45 | ISC | |
| 1.1.3 | ^2.5.16 | ISC | |
| 1.1.2 | ^2.5.16 | ISC | |
| 1.1.1 | ^2.5.16 | ISC | |
| 1.1.0 | ^2.5.16 | ISC | |
| 1.0.6 | ^2.5.16 | ISC | |
| 1.0.5 | ^2.5.16 | ISC | |
| 1.0.4 | ^2.5.16 | ISC | |
| 1.0.3 | ^2.5.16 | ISC | |
| 1.0.2 | ^2.5.16 | ISC | |
| 1.0.1 | ^2.5.13 | ISC | |
| 1.0.0 | ^2.5.13 | ISC | |
| 0.9.3 | ^2.3.3 | ISC | |
| 0.9.2 | ^2.3.3 | ISC | |
| 0.9.1 | ^2.3.3 | ISC | |
| 0.9.0 | ^2.3.3 | ISC | |
| 0.8.0 | ^2.3.3 | ISC | |
| 0.7.2 | ^2.3.3 | ISC | |
| 0.7.1 | ^2.3.3 | ISC | |
| 0.7.0 | ^2.3.3 | ISC | |
| 0.6.3 | ^2.3.3 | ISC | |
| 0.6.2 | ^2.3.3 | ISC | |
| 0.6.1 | ^2.3.3 | ISC | |
| 0.6.0 | ^2.3.3 | ISC | |
| 0.5.2 | ^2.3.3 | ISC | |
| 0.5.1 | ^2.3.3 | ISC | |
| 0.5.0 | ^2.3.3 | ISC | |
| 0.4.1 | ^2.3.3 | ISC | |
| 0.4.0 | ^2.3.3 | ISC |
Showing the latest 30 of 40. See every release on npm
Pre-releases (6)
| Version | Released | Vue | License |
|---|---|---|---|
| 3.1.0-1 | ^3.2.45 | ISC | |
| 3.1.0-0 | ^3.2.45 | ISC | |
| 3.0.1-0 | ^3.2.45 | ISC | |
| 3.0.0-2 | ^3.2.45 | ISC | |
| 3.0.0-1 | ^3.2.45 | ISC | |
| 3.0.0-0 | ^3.2.45 | ISC |