Vue---请求数据--resource

mian.js

//使用2.1导入vue-resource
import VueResource from 'vue-resource'
//2.2安装vue-resource
Vue.use(VueResource)
//2.3设置请求的根路径
Vue.http.options.root = 'http://vue.syudent.io';
//2.4设置post提交表单时的json格式
Vue.http.options.emulateJSON = true;
------------------------------
页面引用
  // vur-resource请求
methods: {
  getNewList() {
      
 this.$http.get("地址").then(res => {
      console.log(res.body);
    if (res.body.satus === 0) {
      this.lunbotu = res.body.message;
    }else{
      //失败
      Toast("加载轮播图失败...");
    }
    })
}
 

}
  需要在生命周期定义调用这个函数
created(){
this.getNewList();
}
 
 
 
-----------------------
或者
   async getlunbotu() {
      // 获取轮播图的方法
      const { data } = await this.$http.get("/api/getlunbo");
      if (data.status === 0) this.lunbotu = data.message;
    }
  },
原文地址:https://www.cnblogs.com/fdxjava/p/11634032.html