1.首先在终端中下载axios
使用 npm:
npm install axios |
使用 bower:
bower install axios |
使用 cdn:
<script src="https://unpkg.com/axios/dist/axios.min.js"></script> |
2.在main.js中配置axios
import http from 'axios'
需要注意 axios不是一个插件 需要绑定在vue的prototype中 给它取名为$http
Vue.prototype.$http = http
3.在文件中使用axios,一般写在生命周期钩子中
mounted() {
this.$http.get('/user?ID=12345')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
},