Vue Resource HTTP Client
vue-resource is an HTTP client plugin for Vue.js, designed for making web requests and handling responses using XMLHttpRequest or JSONP. It supports the Promise API, URI Templates, and interceptors for both requests and responses. The package, currently at version 1.5.3 (last updated in 2021), was primarily built for Vue 1.x and Vue 2.x applications. It boasts compatibility with modern browsers (Firefox, Chrome, Safari, Opera, IE9+) and a compact size (14KB, 5.3KB gzipped). While it was once considered a de-facto 'official' HTTP client, its maintenance has largely ceased, especially with the advent of Vue 3, which encourages the use of alternatives like Axios or the native Fetch API. Its release cadence was irregular towards the end of its active lifecycle, with its last update occurring several years ago.
Common errors
-
Property '$http' does not exist on type 'Vue'.
cause The `vue-resource` plugin was not properly installed or registered with the Vue instance before usage, or TypeScript is not aware of the augmented `$http` property on the Vue prototype.fixEnsure `Vue.use(VueResource)` is called before creating your Vue instance. For TypeScript, add a declaration file (e.g., `vue-shims.d.ts`) to augment the `Vue` type with `$http`: `declare module 'vue/types/vue' { interface Vue { $http: typeof VueResource.http; } }`. -
ReferenceError: Vue is not defined
cause This typically happens in a CommonJS (require) environment or when the Vue library is not globally available (e.g., in a browser via a `<script>` tag) before `vue-resource` attempts to register itself.fixIf using CommonJS, ensure `Vue` is imported and defined before `Vue.use(VueResource)`. If in a browser, confirm Vue is loaded via a `<script>` tag before `vue-resource`, or use an ES module setup with a bundler. -
TypeError: Cannot read property 'then' of undefined
cause This error often occurs when `this.$http` is accessed outside of a Vue component context, or when `vue-resource` has failed to install correctly, leading `this.$http` to be `undefined`.fixVerify that the code accessing `this.$http` is within a Vue component method, lifecycle hook, or a computed property. Confirm `Vue.use(VueResource)` is executed at the application's entry point before any components are initialized.
Warnings
- breaking The `progress` option for tracking upload/download progress was deprecated in v1.5.0. Developers should now use `uploadProgress` and `downloadProgress` options for more granular control over progress events.
- breaking In v1.4.0, the mechanism for passing responses in interceptors changed. Instead of calling `next(...)`, interceptors are now expected to `return` the response or a promise resolving with the response.
- gotcha vue-resource is primarily designed for Vue 1.x and 2.x. It is not officially compatible with Vue 3. Using it in a Vue 3 project will lead to significant compatibility issues and is strongly discouraged.
- deprecated The package has not seen significant updates or active maintenance since 2021 (last release 1.5.3 in June 2021). Its 'official recommendation' status was retired by the Vue core team in 2016.
Install
-
npm install vue-resource -
yarn add vue-resource -
pnpm add vue-resource
Imports
- VueResource
const VueResource = require('vue-resource'); Vue.use(VueResource);import VueResource from 'vue-resource'; Vue.use(VueResource);
- $http
import { $http } from 'vue-resource';this.$http.get('/someUrl').then(response => { // ... }); - VueResourcePlugin
import VueResource from 'vue-resource'; // In a TypeScript file where Vue.$http type needs augmentation declare module 'vue/types/vue' { interface Vue { $http: typeof VueResource.http; } }
Quickstart
import Vue from 'vue';
import VueResource from 'vue-resource';
Vue.use(VueResource);
new Vue({
el: '#app',
data: {
message: 'Loading data...',
someData: null,
error: null,
},
mounted() {
// Example GET request
this.$http.get('https://jsonplaceholder.typicode.com/todos/1')
.then(response => {
// Handle success
this.message = 'Data loaded!';
this.someData = response.body; // response.body contains the parsed data
}, response => {
// Handle error
this.message = 'Error loading data.';
this.error = response.statusText;
console.error('Request failed:', response);
});
// Example POST request
this.$http.post('https://jsonplaceholder.typicode.com/posts', { title: 'foo', body: 'bar', userId: 1 })
.then(response => {
console.log('POST successful:', response.body);
})
.catch(error => {
console.error('POST failed:', error);
});
},
template: `
<div id="app">
<h1>Vue Resource Example</h1>
<p>{{ message }}</p>
<div v-if="someData">
<h2>Fetched Data (GET)</h2>
<pre>{{ JSON.stringify(someData, null, 2) }}</pre>
</div>
<div v-if="error">
<p style="color: red;">Error: {{ error }}</p>
</div>
</div>
`,
});