vue 进行ajax请求,使用axios

1.安装  

   npm install  axios -save

 npm install  vue-axios -save 

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

首先在主入口文件main.js中引用:

import axios from 'axios'
import VueAxios from 'vue-axios'

Vue.use(VueAxios,axios);

3.之后就可以使用了,在组件文件中的methods里去使用了:


getNewsList(){
      this.axios.get('api/getNewsList').then((response)=>{
        this.newsList=response.data.data;
      }).catch((response)=>{
        console.log(response);
      })
}

猜你喜欢

转载自www.cnblogs.com/ygyy/p/10315652.html