VUE 之 axios 使用方法及封装
2019-09-27 16:38
736 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ws19900201/article/details/101543000
- main.js中引入 axios ;
import axios from "axios"; Vue.prototype.axios = axios;
vue文件中使用:
this.axios({ method: "get", url: '/api/url123', params: { area_id: 3, page: this.pagenum } }).then(res => { console.log(res); //todosomething })
2.目前在使用的
新建一个 axios.js 放相同部分的rul;
import axios from "axios"; axios.defaults.baseURL = "http://192.168.0.101/api"; //服务器路径 export default axios;
新建一个 util 文件夹 其中新建一个 fetch.js
import axios from "@/axios"; //引入 刚建的axios文件 import { Message } from "element-ui"; // 创建axios实例 const service = axios.create({ baseURL: axios.defaults.baseURL, // api的base_url timeout: 120000 // 请求超时时间 }); // request拦截器 service.interceptors.request.use( config => { // config.headers["token"] = window.sessionStorage.getItem("tokens"); // config.headers["Content-Type"] = "application/json"; return config; }, error => { // Do something with request error // console.log(error); // for debug Promise.reject(error); } ); // respone拦截器 service.interceptors.response.use( response => { /** * status为非200是抛错 可结合自己业务进行修改 */ const res = response.data; if (res.status != 200) { Message({ message: res.message, type: "warning", duration: 5 * 1000 }); } return res; }, error => { // console.warn("大概是服务器崩溃了,请联系攻城狮"); // for debug Message({ message: "服务器异常,请稍后重试", type: "error", duration: 5 * 1000 }); return Promise.reject(error); } ); export default service;
新建一个 api 文件夹存放URL 其中新建一个 url.js
import fetch from "@/util/fetch"; export function getBanner(params) { //添加轮播 return fetch({ url: "/getbanners", //接口路径 method: "get", params }); } export function getnews02(params) { //添加轮播 return fetch({ url: "/getNewsList", method: "get", params }); }
.vue 文件中使用 ;
import { getnews02 } from "@/api/url.js"; ... created() { this.getSpecialData(); }, methods: { getSpecialData() { const params = { area_id: 3, page: this.pagenum }; getnews02(params).then(res => { // if (res && res.status === 200) { // console.log(res); if (this.pagenum == 1) { this.datacounts = res.count; this.newsTop = res.data.topnew; this.newsList = res.data.list; this.totalnum = parseInt(this.datacounts / 10); } else { this.newsList = [...this.newsList, ...res.data.list]; this.scroll = true; } ++this.pagenum; console.log(this.pagenum); // } }); } ...
相关文章推荐
- 基于vue-cli的vue项目之axios的使用5--axios方法发送请求
- vue中使用axios+Promise二次封装ajax请求
- vue-cli的axios使用方法,get、post请求的跨域问题解决
- 使用async await 封装 axios的方法
- vue中使用axios处理post方法导出excel表格(后端返回文件流)
- vue axios数据请求及vue中使用axios的方法
- vue项目使用axios发送请求让ajax请求头部携带cookie的方法
- Vue使用axios出现options请求方法
- 在 Vue 结合 Axios 使用过程 中 post 方法,后台无法接受到数据问题
- vue-cli中使用微信JS-SDK开发(axios+promise封装)
- vue 里面使用axios 和封装的示例代码
- 在Vue中使用axios请求拦截的实现方法
- Vue官方推荐AJAX组件axios.js使用方法详解与API
- vue中使用的提交axios使用方法
- vue中使用axios post上传头像/图片并实时显示到页面的方法
- vue+axios方法封装(restful,ajax)
- Vue.js插件axios——封装一个可以灵活使用的ajax
- vue-axios封装get方法和post方法
- vue2.0设置proxyTable使用axios进行跨域请求的方法
- 在vue组件中使用axios的方法