vue学习笔记 对接后台服务器

https://blog.csdn.net/yelvgou9995/article/details/106424661/


我们来安装axios(http://www.axios-js.com/),axios是一个基于 promise 的 HTTP 库,这样我们进行前后端对接的时候,使用这个工具可以提高我们的开发效率。

npm install axios --save

然后同样我们在main.js中全局引入axios。

import axios from "axios";

Vue.prototype.$axios =axios

这种做法在****.vue里面回报错:

this.axios.get('http://localhost:9000/getItemList')

TypeError: this.axios is undefined

解决办法:

import axios from 'axios'

import VueAxios from "vue-axios";

Vue.use(VueAxios , axios )

Vue.prototype.$axios =axios

vue-axios是按照vue插件的方式去写的。那么结合vue-axios,就可以去使用vue.use方法了

Vue.use的顺利如果变了的话,还是回报错?

------------------------------------------------------------------------------------------------------------------------------------------------

axios get url 实例

getData(){

let arr = {

id: ['itemOne', 'itemTwo']

}

this.$axios({

// 默认请求方式为get

  method:'get',

  url:'http://localhost:9000/getData',

  // 传递参数

  params: {

 key: arr

  },

  responseType:'json'

}).then(response => {

// 请求成功

  this.res =JSON.stringify(response.data)

}).catch(err =>{

console.log(err)

})

}

created(){

this.getData();

setTimeout(()=>  {

this.dosomething(); //wait axios get what you want!

},500);

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容