Vue Axios Encapsulation Global use

Source: Internet
Author: User

"No ink directly on the Code series"

The code is divided into three steps:

1. Create a api.js, package Axios

2. Introduced in Main.js and added to the Vue prototype

3. Global use

1. Create + Package

//Api.jsImport Axios from "Axios";varApiurl = ' ';functionYuanajax (url,data,successcallback,errorcallback) {axios.post (Apiurl+ url,data). Then (function(res) {if(typeofSuccesscallback = = ' function ') {successcallback (res); }    })    .Catch(function(res) {if(errorcallback) {errorcallback (res); }})}export {Yuanajax}

2. Introduction + Add to Prototype

 

Import vue from ' Vue 'Import App from'./app 'Import Router from'./router '//introducedImport {Yuanajax} from './api 'Import Elementui from' Element-ui '; Import' Element-ui/lib/theme-chalk/index.css '; Import' @/assets/css/common.css '; Import' @/assets/css/font-awesome.min.css '; Vue.use (Elementui); Vue.config.productionTip=false;//Add to prototypeVue.prototype.yuanAjax =Yuanajax;NewVue ({el:' #app ', router, components: {App}, Template:' <App/> '})

3. Use

// Use this. Yuanajax ('/test ', {          A:1},function  (res) {  Console.log ( RES);})

Simple point, suitable for anxious items, haha

Vue Axios Encapsulation Global use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.