vue-axios-plugin
axios plugin for Vuejs project
How to install
Script tag
<!-- add it after vue.js -->
CommonJS
npm install --save vue-axios-plugin
And in your entry file:
Vue
Options
Except axios default request options, vue-axios-plugin
provide below request/response interceptors options:
Name | Type | Default | Description |
---|---|---|---|
reqHandleFunc |
{Function} |
config => config |
The handler function for request, before request is sent |
reqErrorFunc |
{Function} |
error => Promise.reject(error) |
The error function for request error |
resHandleFunc |
{Function} |
response => response |
The handler function for response data |
resErrorFunc |
{Function} |
error => Promise.reject(error) |
The error function for response error |
Usage
Default method in $http
, it just contains get and post method:
this$httpthis$http
Use axios original method in $axios
, by this, you can use all allowed http methods: get,post,delete,put...
this$axios this$axios
ChangeLog for v1.3.0
Before v1.3.0, it send a request use application/x-www-form-urlencoded
format by default, so it config transformRequest
for post request by default, but it is unreasonable. So in v1.3.0, I remove it, all configuration depends on yourself, so you can config the Content-Type
and transformRequest
depend on your backend service.
TODO
- [] Unit test.
Notice!!!
When you send a request use application/x-www-form-urlencoded
format, you need to use qs library to transform post data, like below:
this$http
But if the data
has properties who's type if object/array, you need convert these properties into JSON string:
{ // type check if !obj || typeof obj !== 'object' return obj Object return obj} this$http
More usage, view axios
License
MIT