vue中的请求接口的两种方法
vue 项目安装axios:
1 2 3
   | cnpm install axios --save
  import axios from 'axios'    哪个文件需要用到就在哪个文件中引入
   | 
 
安装qs:
1 2 3
   | npm install --save axios vue-axios qs
   import qs from 'qs'  qs 用来解决vue中post请求以 a=a&b=b 的格式
   | 
 
main.js内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
   |  import Vue from 'vue' import App from './App' import router from './router' import store from './store'
  import axios from 'axios'  (两句很重要) import qs from 'qs'     (两句很重要)
 
  Vue.prototype.$axios=axios  (两句很重要) Vue.prototype.$qs=qs    (两句很重要)
 
  Vue.config.productionTip = false
 
  new Vue({   el: '#app',   router,   store,   components: { App },   template: '<App/>' })
 
  | 
 
需要的页面请求接口时 get 和post 两种方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
   |  loadData(){                          var _this=this;             _this.$axios.get(`../static/tsconfig.json`)               .then(res=> {                 let data=res.data.data;                 console.log(data.description)               })               .catch(error=>{                 console.log(error)               })           },           loadPost(){                          var _this=this;             _this.$axios.post(               `../static/tsconfig.json`,               _this.$qs.stringify({                 sname:'hahaha'               })             )               .then(res=> {                 let data=res.data.data;                 console.log(data.description)               })               .catch(error=>{                 console.log(error)               })           },
 
  | 
 
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 fortunate!