Achieve the website similar vue.use (element) method, self-built tall on the component

In the method implemented using the method of packaging vue example of the realization of axios

import axios from 'axios' // Import Axios package 
axios.defaults.baseUrl = "localhost: 8080"; 
axios.interceptors.request.use ((config) => { 
    the let = localstorage.getItem token ( "token"); / / take local token 
    IF (token) { 
    config.headers.token = "token"; // set carried in the header token; 
} 
return config; // remember config returns, otherwise you will request data error 
}); // a simple axios the package has been achieved, the next to be implemented to achieve vue.use () effect 

const install = (vue) = { // the core, by the install event register; 
    IF (install.installed) { 
     return; // returns the next step 
} 
    Object.defineProperties (VUE, { 
$ API () { 
return Axios; 
} 
}) 
} 
Export the install default;

//main.js, the following references can be achieved vue.use (the effect o) in the main.js 
import api from 'axios package directory' 
vue.use (API) // effect achieved is not very easy

  

Guess you like

Origin www.cnblogs.com/uimeigui/p/11788479.html